I have an array of objects and I want to convert it to a specific array of types. I have a type to convert to which I get at runtime. But I had a problem with the actual conversion.
If I use Convert.ChangeType, I get an error that Object should implement IConvertible
Array.ConvertAll is template-based and requires that I pass the destination type as a template, which I only know at runtime. I even tried using reflection to call this method, but I cannot pass the lambda expression as an argument to MethodInfo.Invoke.
Any ideas?
Correctly I have the following which does not work:
Type convertTo = GetFinalType();
Object[] objArr = GetObjectArray();
var arr = Array.ConvertAll(objArr,elem=> Convert.ChangeType(elem,convertTo));
shake source
share