sharing about .NET and technology RSS 2.0
 Sunday, October 14, 2007

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.

Copy Code
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 } } }
Sunday, October 14, 2007 10:49:11 PM (Romance Standard Time, UTC+01:00)  #    Comments [1] -

Navigation
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Christoph De Baene
Sign In
Statistics
Total Posts: 151
This Year: 22
This Month: 1
This Week: 0
Comments: 147
All Content © 2008, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene (delarou)