Nullable布尔值上的GetType [英] GetType on Nullable Boolean

查看:98
本文介绍了Nullable布尔值上的GetType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在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()得到相同的结果.但这不会发生:

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(将无值的可空类型装箱的结果.)

You're calling GetType on a NULL Reference (The result of boxing a Nullable Type with No Value).

bool? b = new bool?();等同于bool? b = null;

尝试此操作以获得正确的结果:

Try this to get the correct result:

bool? b = new bool?(false);
Console.Write(b.GetType()); // System.Boolean

文档意味着,如果您成功地对具有值(不是Null)的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.

要清除= nullnew 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>

这篇关于Nullable布尔值上的GetType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆