c# - Convert Object Array to another type array using Reflection -
i have object array , want convert specific type array. have type convert @ run time. having problem doing actual conversion.
if use convert.changetype, error object must implement iconvertible
the array.convertall template based , requires pass destination type template know @ run time. tried use reflection call method cannot pass lambda expression argument methodinfo.invoke method.
any ideas?
right have following not working:
type convertto = getfinaltype(); object[] objarr = getobjectarray(); var arr = array.convertall(objarr,elem=> convert.changetype(elem,convertto));
have tried this?
var finaltype = getfinaltype(); var objarr = getobjectarray(); var arr = array.createinstance(finaltype, objarr.length); array.copy(objarr, arr, objarr.length);
that's untested, should work. it's more compact , doesn't (really) use reflection.
Comments
Post a Comment