Home > Net >  Passing dynamic Type at runtime in Array.ConvertAll
Passing dynamic Type at runtime in Array.ConvertAll

Time:05-23

Is it possible to pass Dynamic Type in Array.ConvertAll? in the below code instead of Int32.Parse I tried passing or is there any other way to convert my array of values to another data type

var criteria = Array.ConvertAll(filter.Value, Int32.Parse);

MemberExpression member = Expression.Property(param, filter.Name);
var propertyType = ((PropertyInfo)member.Member).PropertyType;

var criteria = Array.ConvertAll(filter.Value, propertyType); // Gives error

CodePudding user response:

If you want to convert type from an array but we want to determine the type in runtime.

we can try to use Convert.ChangeType method in the second parameter from Array.ConvertAll

var criteria = Array.ConvertAll(arr, (o)=> Convert.ChangeType(o,propertyType)); 
  • Related