问题描述
当我在 Microsoft MSDN 上发现这篇文章时,我正在研究可空布尔值
I was looking into nullable bools when I found this article on Microsoft MSDN
如何:识别可空类型(C# 编程指南)
您可以使用 C# typeof 运算符来创建表示 Nullable 类型的 Type 对象.
You can use the C# typeof operator to create a Type object that represents a Nullable type.
所以我尝试使用可为空的布尔值进行检查:
So I tried checking with a nullable bool:
Console.Write(typeof(bool?)); //System.Nullable`1[System.Boolean]
MSDN上的文章说
您还可以使用 System.Reflection 命名空间的类和方法来生成表示 Nullable 类型的 Type 对象.但是,如果您尝试在运行时使用 GetType 方法或 is 运算符从 Nullable 变量获取类型信息,则结果是表示底层类型的 Type 对象,而不是 Nullable 类型本身.
You can also use the classes and methods of the System.Reflection namespace to generate Type objects that represent Nullable types. However, if you try to obtain type information from Nullable variables at runtime by using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself.
在 Nullable 类型上调用 GetType 会导致在类型隐式转换为 Object 时执行装箱操作.因此 GetType 总是返回一个代表底层类型的 Type 对象,而不是 Nullable 类型.
Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.
如果这是真的,我希望从 .GetType()
获得相同的结果,无论我使用可空 bool 还是常规 bool.但事实并非如此:
If this is true I expect to get the same result from .GetType()
whether I use a nullable bool or a regular bool. But this is not what happens:
bool a = new bool();
Console.Write(a.GetType()); //Prints System.Boolean
bool? b = new bool?();
Console.Write(b.GetType()); //Exception!
发生的异常:
BoolTest.exe 中发生类型为System.NullReferenceException"的未处理异常
An unhandled exception of type 'System.NullReferenceException' occurred in BoolTest.exe
附加信息:未将对象引用设置为对象的实例.
Additional information: Object reference not set to an instance of an object.
但是对象引用被设置为一个对象的实例.导致此错误的原因可能是什么?
But the object reference is set to an instance of an object. What could be the cause of this error?
推荐答案
您正在对 NULL
引用调用 GetType
(使用 No值).
You're calling GetType
on a NULL
Reference (The result of boxing a Nullable Type with No Value).
布尔值?b = new bool?();
等价于 bool?b = null;
试试这个以获得正确的结果:
Try this to get the correct result:
bool? b = new bool?(false);
Console.Write(b.GetType()); // System.Boolean
文档意味着,如果您在具有值(非空)的 Nullable
对象上成功调用 GetType()
.你得到的底层类型是 System.Boolean
.但是您不能使用 NULL
引用调用任何方法,这是适用于任何引用类型的一般规则.
The documentation means that if you call GetType()
successfully on a Nullable
object that has value (Not Null). You get the Underlying type which is System.Boolean
. But you can't call any method using a NULL
reference and this is a general rule that applying to any reference type.
要清除 = null
和 new bool?()
之间的等价点,请检查此 小提琴.两者都生成相同的 IL:
To clear the equivalence point between = null
and new bool?()
, check this Fiddle. Both generates the same IL:
IL_0001: ldloca.s V_0
IL_0003: initobj valuetype [mscorlib]System.Nullable`1<bool>
和
IL_0009: ldloca.s V_1
IL_000b: initobj valuetype [mscorlib]System.Nullable`1<bool>
这篇关于可空布尔值上的 GetType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!