可空性与可空的局部变量 [英] Nullable properties vs. Nullable local variables

查看:134
本文介绍了可空性与可空的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对可空类型的以下行为大惑不解:

 类识别TestClass {
公众诠释?值= 0;
}

测试的TestClass =新识别TestClass();

现在, Nullable.GetUnderlyingType(test.value)返回底层可空类型,这是 INT
但是,如果我尝试获取字段类型像这样

 字段信息字段= typeof运算(识别TestClass).GetFields( BindingFlags.Instance | BindingFlags.Public)[0]; 

和我调用

  Nullable.GetUnderlyingType(field.FieldType)的ToString()

它返回 System.Nullable [System.Int32] 键入。因此,这意味着该方法 Nullable.GetUnderlyingType()有不同的效果,这取决于你如何获取成员类型。为什么会这样?如果我只是使用 test.value 我怎么能告诉它的可空不使用反射?


解决方案

可空类型都有点怪异。但是,至少他们的行为是有据可查的。



从C#编程指南MSDN上,
如何:确定可空类型at的 http://msdn.microsoft.com/en-us/library/ms366789(VS.80)的.aspx




。您还可以使用类和System.Reflection命名空间的方法来生成代表可空类型类型的对象。但是,如果你试图获得可空变量的类型信息在运行时使用GetType方法或者是运营商,其结果是代表底层类型,而不是可空类型本身的Type对象。




上可空类型的GetType调用时造成的类型隐式转换为对象要进行装箱操作。因此的GetType始终返回表示底层型,而不是可空类型的Type对象。


值得指出的是,在您的标题的区别是不准确的。类型的行为是不鲜明基于本地变量或特性,但在该类型是否经由运行时对象或由反射(或使用typeof运算符的)访问。你的推理是可以理解的,因为局部变量的类型通常只有一个运行时对象来访问的,但是,它是有缺陷的,因为如果你通过属性访问器在运行时访问一个可空的对象,然后它的行为将等同于中。一个局部变量



此外,为了明确地回答你的问题的最后一部分:告诉test.value唯一的办法就是不使用反射将可为空访问它,并得到一个NullReferenceException (其中,当然,如果test.value为空时,才会发生。正如所写的,在你的榜样,的值不为空,所以确定本无反射会不可能


I am puzzled by the following behavior of Nullable types:

class TestClass {
    public int? value = 0;
}

TestClass test = new TestClass();

Now, Nullable.GetUnderlyingType(test.value) returns the underlying Nullable type, which is int. However, if I try to obtain the field type like this

FieldInfo field = typeof(TestClass).GetFields(BindingFlags.Instance | BindingFlags.Public)[0];

and I invoke

Nullable.GetUnderlyingType(field.FieldType).ToString()

it returns a System.Nullable[System.Int32] type. So that means the method Nullable.GetUnderlyingType() has a different behavior depending on how you obtain the member type. Why is that so? If I simply use test.value how can I tell that it's Nullable without using reflection?

解决方案

Nullable types are a little bit weird. However, at least their behavior is well documented.

From the C# programming guide on MSDN, "How to: Identify Nullable Types"at http://msdn.microsoft.com/en-us/library/ms366789(VS.80).aspx :

You can also use the classes and methods of the System.Reflection namespace to generate Type objects that represent Nullable types. However, if you attempt to obtain type information from Nullable variables at runtime 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.

It's worth pointing out that the distinction in your headline is inaccurate. The type behavior is not distinct based on local variables or properties, but on whether the type is accessed via a runtime object or by reflection (or use of the typeof operator). Your inference is understandable, since the types of local variables are usually only accessed by means of a runtime object, however, it is flawed, because if you access a nullable object at runtime via a property accessor then its behavior will be equivalent to that of a local variable.

Also, to answer the last part of your question explicitly: the only way to tell that test.value is nullable without using reflection would be to access it and get a NullReferenceException (which, of course, can only happen if test.value is null. As written, in your example, the value is not null, so determining this without reflection would be impossible

这篇关于可空性与可空的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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