How to get the type Nullable <Enum>?
How can you do something like the following in C #?
Type _nullableEnumType = typeof(Enum?); I think the best question is why you cannot do it when you can do it:
Type _nullableDecimalType = typeof(decimal?); +7
devlife
source share1 answer
Enum not an enumeration - it is a base class for enumerations and is a reference type (i.e. a class ). Enum? that mean Enum? is illegal because Nullable<T> has a limit on T : struct , and Enum does not satisfy this.
So: use typeof(Nullable<>).MakeGenericType(enumTypeKnownAtRuntime) or, more simply, typeof(EnumTypeKnownAtCompileTime?)
You may also notice that:
Enum x = {some value}; is a boxing operation , so usually you should avoid using Enum as a parameter, etc.
+14
Marc gravell
source share