Retrieving a typedef for resettable types allows code breaks for non-nullable types

This is the next question to the next one if you want: Parsing for Nullable Enum

Type t = currentProperty.PropertyType;
if (t.GetGenericTypeDefinition() == typeof(Nullable<>))
    t = t.GetGenericArguments().First();


I am getting an error on line # 2 which is an IF statement.

System.Reflection.TargetInvocationException : Exception has been thrown by the 
target of an invocation. ----> System.InvalidOperationException : This operation 
is only valid on generic types.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)


How can I check this condition before executing the code in the IF statement?


source to share

1 answer

Below is the MSDN documentation on type checking with null

It looks like you need to add the following:

if(t.IsGenericType && ...




All Articles