gpt4 book ai didi

c# - 可空 boolean 值上的 GetType

转载 作者:太空狗 更新时间:2023-10-30 00:18:19 26 4
gpt4 key购买 nike

当我在 Microsoft MSDN 上找到这篇文章时,我正在研究可为空的 boolean 值

How to: Identify a Nullable Type (C# Programming Guide)

You can use the C# typeof operator to create a Type object that represents a Nullable type.

所以我尝试使用可为 null 的 boolean 值进行检查:

Console.Write(typeof(bool?)); //System.Nullable`1[System.Boolean]

MSDN 上的文章说

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.

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。但事实并非如此:

    bool a = new bool();
Console.Write(a.GetType()); //Prints System.Boolean

bool? b = new bool?();
Console.Write(b.GetType()); //Exception!

发生的异常:

An unhandled exception of type 'System.NullReferenceException' occurred in BoolTest.exe

Additional information: Object reference not set to an instance of an object.

但是对象引用被设置为一个对象的实例。导致此错误的原因可能是什么?

最佳答案

您的实际问题似乎是:

By initializing a Nullable<T> to its default value using its default constructor, for example bool? b = new bool?();, why does accessing b's members such as GetType() throw a NullReferenceException? Doesn't new always return a value, so b can't really be null?

好吧,是的,不是。 Nullable<T>有点特别。来自 C# 规范:

4.1.10 Nullable types

[...]

An instance of a nullable type T? has two public read-only properties:

  • A HasValue property of type bool
  • A Value property of type T

An instance for which HasValue is true is said to be non-null. A non-null instance contains a known value and Value returns that value. An instance for which HasValue is false is said to be null. A null instance has an undefined value. Attempting to read the Value of a null instance causes a System.InvalidOperationException to be thrown.

所以是的,bool? b = new bool?();确实返回一个实例:一个你只能调用HasValue的实例在。因为它返回 false ,您无法对该实例做很多其他事情。

然后是下一个相关部分:

4.3.1 Boxing conversions

Boxing a value of a nullable-type produces a null reference if it is the null value (HasValue is false)

MSDN: Boxing Nullable Types (C# Programming Guide) 中也对此进行了解释:

Objects based on nullable types are only boxed if the object is non-null. If HasValue is false, the object reference is assigned to null instead of boxing.

进一步了解规范:

11.3.5 Boxing and unboxing

When a struct type overrides a virtual method inherited from System.Object (such as Equals, GetHashCode, or ToString), invocation of the virtual method through an instance of the struct type does not cause boxing to occur.

GetType()未被 Nullable<T> 覆盖,所以会发生拳击。当您调用 GetType()或结构上的任何非重写方法,该结构将在调用该方法之前被装箱到一个对象。在空的情况下 Nullable<T> , 该装箱操作的结果将是 (object)null .因此异常(exception)。

另见 Does calling a method on a value type result in boxing in .NET? .


所以,回答你的问题:

  • b null,它包含一个Nullable<bool>HasValue指示false .
  • 调用非覆盖 GetType()方法,导致 Nullable<bool>结构被装箱以访问底层方法 object.GetType() .
  • 这个装箱操作实际上并没有装箱,只是简单地返回了(object)null。 .
  • 最后,您调用了 ((object)null).GetType() ,抛出 NullReferenceException你遇到了。

如果您实际上正在寻找一段可以返回任何变量类型的代码,即使是 null Nullable<T> ,使用这样的东西:

Type GetType<T>(T obj) 
{
return typeof(T);
}

然后你可以这样调用:

Console.WriteLine(GetType(b));

关于c# - 可空 boolean 值上的 GetType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com