Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果typeof(int?)是一个Int32,那么Nullable.GetUnderlyingType有什么用途?_C#_Nullable - Fatal编程技术网

C# 如果typeof(int?)是一个Int32,那么Nullable.GetUnderlyingType有什么用途?

C# 如果typeof(int?)是一个Int32,那么Nullable.GetUnderlyingType有什么用途?,c#,nullable,C#,Nullable,为什么typeof int?anInt32 int? x = 1; Console.WriteLine(x.GetType().Name); 如果可以,那么当您不知道它的Int32时,Nullable.getunderyingtype有什么用呢 例子: public类型GetNullableUnderlyingType(Nullable obj) 其中T:struct { 返回Nullable.getUnderlineType(typeof(Nullable)); } 在这里,您可以传递任何

为什么
typeof int?
an
Int32

int? x = 1;
Console.WriteLine(x.GetType().Name);

如果可以,那么当您不知道它的
Int32
时,
Nullable.getunderyingtype
有什么用呢

例子:
public类型GetNullableUnderlyingType(Nullable obj)
其中T:struct
{
返回Nullable.getUnderlineType(typeof(Nullable));
}

在这里,您可以传递任何
Nullable
对象,并让它返回其基础类型。

当您编写
int?
时,就好像您编写了
Nullable
一样。我想这就是您要找的类型。

这个示例有点混乱,因为:

int? x = 1;
像您期望的那样创建一个
可为空的
;然而:

Type tupe = x.GetType();
是对
对象
上的非虚拟方法的调用,该方法没有(也不能)被覆盖-因此这是一个装箱操作;而
Nullable
有特殊的装箱规则:

  • 如果为空,则将框设置为
    null
  • 如果它有一个值,则将该值装箱并返回
i、 e

框到完全相同

因此,您正在将
typeof(int)
传递给
getunderyingtype

当使用反射时,这有帮助的一个更具说明性的示例是:

class Foo {
    public int? Bar {get;set;}
}
...
Type type = typeof(Foo); // usually indirectly
foreach(var prop in type.GetProperties()) {
     Type propType = prop.PropertyType,
          nullType = Nullable.GetUnderlyingType(propType);

     if(nullType != null) {
         // special code for handling Nullable<T> properties;
         // note nullType now holds the T
     } else {
         // code for handling other properties
     }
}
class-Foo{
公共int?Bar{get;set;}
}
...
Type Type=typeof(Foo);//通常间接地
foreach(type.GetProperties()中的var prop){
类型propType=prop.PropertyType,
nullType=Nullable.GetUnderlyingType(propType);
if(null类型!=null){
//处理可空属性的特殊代码;
//注意nullType现在保存了T
}否则{
//处理其他属性的代码
}
}
调用
GetType()
框选变量。CLR有一个特殊规则,
Nullable
被装箱到
T
。因此
x.GetType
将返回
Int32
,而不是
Nullable

引述:

基于可空类型的对象仅在对象为非空时才装箱。如果
HasValue
false
,则将对象引用指定给
null
,而不是装箱

如果对象为非null(如果
HasValue
true
),则会进行装箱,但只装箱可空对象所基于的基础类型。装箱一个非空的可空值类型会装箱值类型本身,而不是包装该值类型的
System.nullable


主要用于处理泛型方法: e、 g

publicstaticvoidsomethod(T参数)
{
if(可为null.GetUnderlineType(typeof(T)!=null)
{
/*可为空代码的特殊情况,请转到此处*/
}
其他的
{
/*做些其他事情,它不可为空*/
}
}

知道这一点很重要,因为某些非常便宜的东西在nullable上可能非常昂贵。例如,
if(argument==null)
通常非常便宜,但是在
Nullable
上使用泛型方法时,必须将
参数
框起来以获得一个空引用。最好的办法是使用
EqualityComparer.Default
,这将减慢其他所有操作,但使Nullable不受影响。

我认为问题是为什么代码会编写空引用t“Int32”而不是“Nullable”,我想他的意思是:如果调用
x.GetType().Name
可以得到
Int32
,那么在上面的例子中,为什么我们需要Nullable.getUnderlineType()来找出它的
Int32
。假设为false-
typeof(int?)
System.Nullable
。确切地说,它不应该是像我为List as List'1
GetUnderlinegtype
-我当时最喜欢的打字类型那样的Nullable'1吗?而且,
tupe
@Marc:实际上,
GetType
不是
虚拟的,这就是为什么它需要装箱的原因(它调用
Object.GetType
的实现,这要求接收方的类型为
Object
)。如果它是虚拟的,并且在派生类中被重写,则不需要装箱。@Mehrdad-我的术语不清楚;它是一个virtcall,但它是一个解析为object的非虚拟方法,因此需要装箱。virtcall也需要装箱到一个未被重写的虚拟方法。我知道你知道-我只是在添加完整上下文t;pyu的意思是x.GetType()和x.Value.GetType()是一样的吗?奇怪吧。当你对某个返回nullable类型的方法进行反射时,你会得到类似nullable'1@Mubashir如果您认为这很奇怪,那么它还有另一个后果:这里的问题是:要在常规代码中调用该方法,必须在调用方解决泛型(为了匹配签名)他们必须已经知道它是可空的等等。如果他们已经知道…他们为什么要问?@PeterLillevold我想这已经完成了(也许在你的评论之后),但是在任何情况下,协作环境也是如此-非常欢迎你编辑答案(或问题)你自己和格式化它。@OferZelig是的,我很清楚这一点。我想在2010年我只是想给CodesInChaos一个这样做的机会:)
int? x = 1;
int y = 1;
class Foo {
    public int? Bar {get;set;}
}
...
Type type = typeof(Foo); // usually indirectly
foreach(var prop in type.GetProperties()) {
     Type propType = prop.PropertyType,
          nullType = Nullable.GetUnderlyingType(propType);

     if(nullType != null) {
         // special code for handling Nullable<T> properties;
         // note nullType now holds the T
     } else {
         // code for handling other properties
     }
}
int? x = 1;
x.GetType() //Int32
typeof(int?) //Nullable<Int32>
int? x = null;
x.GetType() //throws NullReferenceException
public static void SomeMethod<T>(T argument)
{
     if(Nullable.GetUnderlyingType(typeof(T) != null)
     {
             /* special case for nullable code go here */
     }
     else
     {
            /* Do something else T isn't nullable */
     }
}