• Blog
  • About
  • Contact
linkedin
twitter

Blog Post

14
OCT
2007

Reflection and Generics

tag : .NET SDK
by : Christoph De Baene
comment : 1

On my current project I had the need to iterate through the properties of an object with reflection and to check if one of the properties is a generic List type, e.g. IList<int>, IList<Customer>, etc. To check through reflection on a generic type, you need to use the GetGenericTypeDefinition method.

[csharp]
foreach (PropertyInfo propertyInfo in entity.GetType().GetProperties())
{
if (propertyInfo.PropertyType.IsGenericType &&
typeof(List<>).IsAssignableFrom
(propertyInfo.PropertyType.GetGenericTypeDefinition()))
{
IEnumerable enumerable = propertyInfo.GetValue(entity, null) as IEnumerable;
IEnumerator enumerator = enumerable.GetEnumerator();

while (enumerator.MoveNext())
{
// do something
}
}
}
[/csharp]

About the Author

Social Share

    One Comment

    1. Michael Finlan October 17, 2007 at 6:24 pm Log in to Reply

      Hi Delarou,

      SuperFandango is a .NET(mainly) and database driven applications specialist company based in Bruxelles.

      At the moment, we have a need for extra .net/sql server/ classic asp + vb6 skills.

      We came across you via some user group listing.

      We would like to get a copy of your CV along with your contact details (email and phone). So thanks to email them to michael.finlan@superfandango.com .

      If you know any people with Microsoft Technology skills that are looking for a contract or an employee position, could you be so kind as to forward them our contact details.

      Thanks and best regards,

      Michael FINLAN
      8 Av. du GEAI
      1170 Watermael-Boitsfort
      Belgium

      Tel/Fax: +32 2 742 0587
      GSM: +32 486 423 697
      http://www.superfandango.com
      michael.finlan@superfandango.com

    Leave a Reply Cancel reply

    You must be logged in to post a comment.

    Tag cloud

    .NET SDK Ajax Architecture ASP.NET ASP.NET Web API Code Generation Components Events Forms Hardware Modeling Neo4j Patterns & Practices Personal Roslyn Services Silverlight SQL Server Testing Tools Unity Utils & Tools Virtualization Visual studio Windows Live WPF

    Archive

    • April 2012
    • October 2011
    • August 2011
    • April 2011
    • March 2011
    • July 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • August 2008
    • March 2008
    • December 2007
    • November 2007
    • October 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • July 2005
    • June 2005
    • May 2005
    • April 2005
    • March 2005
    • February 2005
    • September 2004
    • July 2004
    • June 2004
    • May 2004
    • April 2004
    • March 2004
      Copyright 2013, Christoph De Baene