How to determine whether a type is an instance of some generic type without knowing T?

Posted by Andrew on March 21, 2007

For example, given a type, here’s how to find out whether it’s a nullable type.

// is myType nullable?
 
bool isNullable = myType.IsGenericType
  && (myType.GetGenericTypeDefinition() 
        == typeof(Nullable<>));
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Ayende RahienNo Gravatar Thu, 22 Mar 2007 04:51:32 CDT

    This would fail for typeof(Nullable)

  2. andrewpetersNo Gravatar Thu, 22 Mar 2007 05:14:07 CDT

    Ah, that would be because System.Nullable isn’t nullable :-)

Comments