How can you do something like the following in C#?
Type _nullableEnumType = typeof(Enum?);
I guess a better question is why can’t you do that when you can do this:
Type _nullableDecimalType = typeof(decimal?);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Enumis not an enum – it is the base-class for enums, and is a reference-type (i.e. aclass). This means thatEnum?is illegal, asNullable<T>has a restriction thatT : struct, andEnumdoes not satisfy that.So: either use
typeof(Nullable<>).MakeGenericType(enumTypeKnownAtRuntime), or more simply,typeof(EnumTypeKnownAtCompileTime?)You might also want to note that:
is a boxing operation, so you should usually avoid using
Enumas a parameter etc.