C# Generic headach

I recently converted some code to use Generic types in C# 2.0 and got the following error:

Cannot create an instance of ... because Type.ContainsGenericParameters is true

Turns out, you can’t dynamically call methods on a generic class. Well you can, but you kind of have to convert the generic type to enable late bound reflection.

type = instance.GetType().MakeGenericType(instance.GetType().GetGenericArguments());
return type.GetProperty(property).GetValue(instance, null);

via (GA)


Leave a comment

Filed under C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s