通过Activator.CreateInstance创建可为空的对象返回null

我正在创建一个将小脚本变成dll的系统。 当我尝试获取可以为空的值类并使其成为参数的默认值时,我遇到了问题。 问题是我需要在编译器中创建一个用户选择为nullable的实例,并将其设置为常量。

不幸的是,每当我使用Activator.CreateInstance(NullableType) (其中NullableType是来自用户输入的创建类型)时,我得到null作为返回值。 例如,只需执行:

 object Test = Activator.CreateInstance(typeof(Nullable)); Console.WriteLine(Test == null); 

返回true。 这只发生在Nullables上。 在C#中创建的结构,甚至是通用结构,都可以很好地创建。 哎呀,如果我从DotNetReflector复制并粘贴nullable(并删除TypeDependencyAttribute,TargetPatchingOptOutAttribute和ThrowHelper调用,因为我没有访问权限),它会在上面显示False。

到底是怎么回事? 当我在运行时之前不知道通用参数时,还有其他可能的方法来创建可空吗?

来自这个MSDN博客 :

永远不会期望Activator.CreateInstance返回null; 使用此DCR,它将在创建Nullable类型的实例时返回null,但不提供非空T值。 例如,Activator.CreateInstance(typeof(Int32?))返回null。

问题与如何将Nullable变量装箱,如博客文章所解释的那样。

问题出在ParameterBuilder.SetConstant ,而不是Activator.CreateInstanceSetConstant是用于定义可选参数的默认值的函数,并且需要提供具体值作为常量。 对于ref类, null是一个有效的具体值,但对于创建Nullable<>之前的值类, null不是有效值。 例如,如何将unbox null转换为int ? 因此, SetConstant检查了值类型,以查看作为常量传入的具体值是否为null并将ArgumentException作为比NullReferenceException更具描述性的错误抛出,以便将null拆分为值类。

Nullable<>的情况下, null现在是值类的有效具体值。 Nullable<>本身是一个值类,如Activator.CreateInstance ,并且将null压缩为Nullable具有意义。 这就是SetConstant有错误的地方:它没有考虑到这一点,并且对于实际上不是错误的内容抛出了“描述性”错误。 代替Microsoft的错误修复,任何使用null Nullable<> SetConstant调用都必须实现由不正确的条件保护的行为。 这意味着使用reflection挖掘ParameterBuilder的私有方法和字段。 这是我为处理这种情况而编写的代码。 标准SetConstant函数应该在不显示错误的情况下使用。

 //ModuleBuilder module : a parameter passed into the containing function, the module which is being built (which contains the method that has the optional parameter we are setting the constant for) //ParameterBuilder optParam : A parameter passed into the containing function, the parameter that is being built which is to have the null default value. MethodInfo method = typeof(TypeBuilder).GetMethods(BindingFlags.Static | BindingFlags.NonPublic) .Where(m => m.Name == "SetConstantValue" && m.GetParameters().Length > 0 && m.GetParameters()[0].ParameterType.Name == "RuntimeModule") .Single(); var RuntimeHandle = typeof(ModuleBuilder).GetMethod("GetNativeHandle", BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(null, new object[] { RuntimeHandle.Invoke(module, new object[]{}), optParam.GetToken().Token, 0x12, null }); 

我已经向微软报告了这个错误。 他们回答称它不会在.NET 3.5中修复,但它被添加到内部错误数据库中。

更新:

该错误已在.NET 4.0中修复。 ParameterBuilder.SetConstant现在有一个constant == null条件的分支,它检查值类型是否是从Nullable<>派生的generics,如果不是则抛出exception。