Link to home
Start Free TrialLog in
Avatar of neudeal
neudeal

asked on

dynamic casting for generic types

hello,

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[] { });

where:
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?

Thanks,
Robert

Avatar of surajguptha
surajguptha
Flag of United States of America image

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 ???
Avatar of neudeal
neudeal

ASKER

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?

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of surajguptha
surajguptha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin