vendredi 29 septembre 2017

How can I cast List

I have a situation where through reflection I have List. I also have a reflected method that takes List. List contains a whole bunch of foos, but List cannot be directly casted to List. I have a Type variable that contains foo, but do not know it at compile time. My code looks something like below

var ASM = Assembly.LoadFrom("foo.dll");
var bar = List<string>() { /*A whole bunch of string literals*/ };

var FooType = ASM.GetType("Foo")
List<object> foos = new List<object>; 
foreach (var string in var)
{
   foos.add(Activator.CreateInstance(FooType, new [] {string}));
}

var Flags = BindingFlags.Static | BindingFalgs.Public;
FooType.GetMethod("DoSomethingWithAListOfFoo", Flags).Invoke(null, foos)

At this point I'm told that a List cannot be converted to a List via a runtime exception





Aucun commentaire:

Enregistrer un commentaire