Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Adapt to array does not work

Open HZ-GeLiang opened this issue 1 year ago • 4 comments

var v_int1 = 1;  
var v1_= v_int1.Adapt<int[]>();//error :“Invalid cast from 'System.Int32' to  ...
var v1_2 = v_int1.Adapt<List<int>>().Adapt<int[]>();//ok

HZ-GeLiang avatar Jul 21 '23 00:07 HZ-GeLiang

Hm, this should indeed work. I'll add a regression test for this and see if I can pinpoint where things go wrong.

andrerav avatar Sep 26 '23 21:09 andrerav

@HZ-GeLiang
You wanted to initialize an array for one element typeof int - int[1] or new int[1] { 1 } ? Now this get

var v1_2 = v_int1.Adapt<List>().Adapt<int[]>();//ok

you get int[0] - empty array for Type int

DocSvartz avatar Sep 28 '23 18:09 DocSvartz

@HZ-GeLiang You wanted to initialize an array for one element typeof int - int[1] or new int[1] { 1 } ? Now this get

var v1_2 = v_int1.Adapt<List>().Adapt<int[]>();//ok

you get int[0] - empty array for Type int

I want is new int [1]{1}

If possible, I would like the result to be:

v_int1.Adapt<List>() // returns new List { 1 } //In Mapster 7.4.0, the return result of this line of code is

new List<int>();

v_int1.Adapt<int[]>() // returns new int[] { 1 }

HZ-GeLiang avatar Sep 30 '23 13:09 HZ-GeLiang

Seems like from Array is work from this algorithm

1.Adapt<List>() - Adapt as Class and this work this:

  1. Inside member of Int is not possible to create any of the available constructors for the List,
  2. create new List() is created.

DocSvartz avatar Oct 01 '23 03:10 DocSvartz