問題描述
當我在 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!