获取可空类型的类型定义会破坏非可空类型的代码。

3

这是一个追加问题,如果您想看见的话可以查看以下内容:将值解析为可空枚举类型

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

我在第2行遇到了一个错误,这是一个 IF 语句。

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)

在执行IF语句代码之前,我该如何测试该条件?

1个回答

6

1
为什么取消了点赞呢?难道这个回答没有解决你的具体问题吗? - Justin Pihony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接