I am trying out a little reflection and have a question on how the cast the result object to an IList.
Here is the reflection:
private void LoadBars(Type barType)
{
// foo has a method that returns bars
Type foo = typeof(Foo);
MethodInfo method = foo.GetMethod("GetBars")
.MakeGenericMethod(bar);
object obj = method.Invoke(foo, new object[] { /* arguments here */ });
// how can we cast obj to an IList<Type> - barType
}
How can we cast the result of method.Invoke to an IList of Type from the barType argument?
The point of a cast is usually to tell the compiler that you have some extra information - that you know something at compile time. You don't know that information here - you only know it at execution time.
What would you expect to do with the value after casting it? Admittedly there are some times when it would be useful - when you've got to use a generic interface, even if you want to get at members which don't require the type parameter (e.g. Count
in IList<T>
). However, if that's not what you're trying to do it would really help if you could give more information.
I've just finished wrestling with this problem.
True, you cannot cast the object into a Generic IList but you can convert it into a strongly typed Array by invoking the "ToArray" method of the List object.
Solution pilfered from another blog. http://amazedsaint.blogspot.com/2008/04/creating-generic-list-at-runtime.html
ToArrayMethod = obj.GetType().GetMethod("ToArray");
System.Array stronglyTypedArray=(System.Array) ToArrayMethod.Invoke(obj,null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With