Here is what I've found on MSDN about this:
property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)
So you basically need to determine if the type is generic (makes sense as nullable is a type that takes T (Nullable
Now, I really wondered do I really need both checks?
As it was coded that way at MSDN I first didn't look at that, but I wanted to tackle it a bit more.
First I thought that the first one is needed to protect you from the null value returned by the GetGenericTypeDefinition() method.
But what's the problem even if it does?
Well, you get InvalidOperationException saying "This operation is only valid on generic types.".
So you definetelly need both checks ;).
It is kind of ugly for me, maybe it will be better to create an extender to the Type type. Something like "IsGeneric()", but the Is prefix always suggests property while this is a method.
Anyway, I'll go with that.
In the next post I'll show you how to get the type out of a Nullable<>.
No comments:
Post a Comment