?
Solved

dynamic casting for generic types

Posted on 2007-10-09
5
Medium Priority
?
707 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:neudeal
  • 2
4 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20044835
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 ???
0
 

Author Comment

by:neudeal
ID: 20044888
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,
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 2000 total points
ID: 20044900
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);
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20285132
Forced accept.

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

807 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