First you'll need to determine if it's Nullable<>
and then you'll need to grab the nullable types:
if (type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof(Nullable<>)
&& type.GetGenericArguments().Any(t => t.IsValueType && t.IsPrimitive))
{
// it's a nullable primitive
}
Now, the aforementioned works, but not recursively. To get it to work recursively you'll need to put this into a method that you can call recursively for all types in GetGenericArguments
. However, don't do that if it's not necessary.
That code may also be able to be converted to this:
if (type.GetGenericArguments().Any(t => t.IsValueType && t.IsPrimitive))
{
// it's a nullable primitive
}
but the caveat there is that it may be a generic reference type and may not actually meet the definition of primitive for your needs. Again, remember, the aforementioned is more concise but could return a false positive.