.net - C# Cast List of MyType to List of objects -
i'm writing application required utilize reflection phone call method has parameters of type myobject.
method (list<myobject> input , out list<myobject> output,..... );
using reflection send parameter of type object. how can cast list<myobject>
list<object>
var parameters = new object[] { inputs, outputs, userprams }; system.type classtype = typeof(myclass); object instance = activator.createinstance(classtype); classtype.invokemember(name, bindingflags.invokemethod | bindingflags.instance | bindingflags.public,null, instance, parameters);
in code above both input , output lists of type myobject
i tried cast them list of objects doesn't work
x.outputs = groutputs ilist<object>
can help?
your question not 100% clear, assumed problem you're facing 1 set in title:
cast list of mytype list of objects
as @charles said, ilist<t>
, list<t>
not variant, can't cast ilist<derivedclass>
ilist<baseclass>
. have create new list<baseclass>
.
there many ways that, think there 2 should consider:
you can utilize cast
, tolist
, require using system.linq
.
var listofstrings = new list<string>() { "foo", "bar" }; var listofobjects = listofstrings.cast<object>().tolist();
to avoid that, can utilize new list<t>(ienumerable<t> source)
constructor. because ienumerable<t>
covariant, can following:
var listofstrings = new list<string>() { "foo", "bar" }; var listofobjects = new list<object>(listofstring);
c# .net reflection casting
No comments:
Post a Comment