dynamic casting for generic types

Posted on 2007-10-09
Last Modified: 2012-08-13

I need to make a cast to IList<T> but I will not know what T is until runtime.  Anyone know the best way to accomplish this... codeDOM perhaps?

Ultimately, I need to execute a statement like the following:

MethodInfo info;
Object obj;
IList<T> res = (IList<T>)info.Invoke(obj, new object[] { });

the cast type will be info.ReturnType;  i.e. info.ReturnType == IList<T>
or T will be info.ReturnParameter.ParameterType;  i.e.  info.ReturnType.ParameterType == T

Does this make sense?


Question by:neudeal
    LVL 21

    Expert Comment

    Let me understand. you are looking to pass an array of strings to a function that should return you a List of type T , probably having the strings you sent as one of its properties ???

    Author Comment

    The method invoked by the MethodInfo instance described here will return an IList<> of some type T.  T could be strings, or foos or bars...  it will be an IList<> of whatever the MethodInfo.ReturnParameter is.

    I guess the question is...  is there a way for me to declare and assign an instance of IList<T> while learning what T is on the fly?

    LVL 21

    Accepted Solution

    Kind of pseudo code, let me know if you need more specific code
                    IList listObject = (IList)_dataSource;
                    Type objectType = listObject[0].GetType();
                    Type genericType = typeof(List<>);
                    Type[] typeArgs = new Type[] { objectType };
                    Type actualType = genericType.MakeGenericType(typeArgs);
                    System.Reflection.ConstructorInfo constructorInfo = actualType.GetConstructor(new Type[] { });
                    System.Collections.IList returnList = constructorInfo.Invoke(null) as System.Collections.IList;
                    System.Reflection.PropertyInfo valueInfo = objectType.GetProperty(ValueMember);
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now