MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.32k stars 329 forks source link

Adapt to array does not work #610

Open HZ-GeLiang opened 1 year ago

HZ-GeLiang commented 1 year ago
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
andrerav commented 1 year ago

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

DocSvartz commented 1 year ago

@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

HZ-GeLiang commented 1 year ago

@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().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 }

DocSvartz commented 1 year ago

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.