miercuri, 17 septembrie 2008

Hidden gems of Generic Types

This post could have been called "How to find that a type is of a certain generic type ?"
The solution is so simple I would like not to tell you about it, but just in case it will help someone here it goes :

typeof(int?).GetGenericTypeDefinition() == typeof(Nullable<>)

The example is from something I needed to find out. Whenever you generate a nullable type it gets converted by the compiler into a Nullable. E.g. int? --> Nullable. So the example above should be suffice to point you in the right direction if you're searching for a way to find out if a certain object is of a nullable type ;).

Enjoy and take care.

Niciun comentariu: