Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

generics and reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
parez - 11 Mar 2008 21:58 GMT
Hi

One of the property of returnedObject is of the type List<MyClass> (in
just one instance)
in some other case it could be List<MyClass2>

I want to check if  a particular propert is a generic list.
If it is, then do something with the type of the the class (MyClass
and MyClass2)

           PropertyInfo[] propertyInfo =
returnedObject.GetType().GetProperties();

           foreach (PropertyInfo pi in propertyInfo)
          {

             // one of the "pi"s  is of the list List<MyClass>
               if (pi.PropertyType == typeof(List<>))
               {
                    //Do Speical stuff

how do i go about this..
Jon Skeet [C# MVP] - 11 Mar 2008 22:03 GMT
> One of the property of returnedObject is of the type List<MyClass> (in
> just one instance)
[quoted text clipped - 16 lines]
>
> how do i go about this..

Take pi.PropertyType, then:

o Check it's generic: type.IsGeneric
o Get the generic type: type.GetGenericType
o Check whether that generic type is List<>
o Fetch the generic type argument: type.GetGenericArguments

That's entirely untested, but I think it should work...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

parez - 11 Mar 2008 22:28 GMT
> > One of the property of returnedObject is of the type List<MyClass> (in
> > just one instance)
[quoted text clipped - 29 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

Thanks..

This is what worked for me.

if (pi.PropertyType.IsGenericType)
               {

                   if (pi.PropertyType.GetGenericTypeDefinition() ==
typeof(List<>))
                   {
                      // pi.PropertyType.GetGenericArguments() is
MyClass2, MyClass

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.