开发者

C# - Cast object to IList<T> based on Type

开发者 https://www.devze.com 2022-12-23 23:22 出处:网络
I am trying out a little reflection and have a question on how the cast the result object to an IList.

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)
{
    //开发者_如何学Python 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);


In .NET 4.0 you can use expression trees to achive that.


private void LoadBars<T>()
{
    Type barType = typeof(T);
    // foo has a method that returns bars
    Type foo = typeof(Foo);

    MethodInfo method = foo.GetMethod("GetBars")
        .MakeGenericMethod(bar);

    IList<T> obj = (IList<T>)method.Invoke(foo, new object[] { /* arguments here */ });
}


Casting would only make sense if the caller of your function new barType at compile time, and not a runtime. And once that is true, you could just template the function to:

private IList<T> LoadBars<T>()
{
...
return obj as IList<T>;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消