Concise code

Yesterday when porting LinqtoCRM to VS 2008 Beta 2, I had to get an array of the types of the properties of some generic type. This array would be passed to Type.GetConstructor(). I had an array of PropertyInfos which contain the type of the property, and I could have new’ed up an array and looped the types into that. Instead I did this:

PropertyInfo[] props = typeof(T).GetProperties();
Type[] types = props.Select(p => p.PropertyType).ToArray();
ConstructorInfo cInf = typeof(T).GetConstructor(types);

Comments

Kristian Dupont on

C# 3.0 is going to be the coolest language ever that has commercial success.
Congrats on your blog – I’m looking forward to frequent enlightenment 🙂

Reply

Leave a Reply to Kristian Dupont Cancel reply

Your email address will not be published. Required fields are marked *