【发布时间】:2010-10-21 14:02:14
【问题描述】:
我正在对可空类型进行一些测试,但它并没有像我预期的那样工作:
int? testInt = 0;
Type nullableType = typeof(int?);
Assert.AreEqual(nullableType, testInt.GetType()); // not the same type
这也不起作用:
DateTime? test = new DateTime(434523452345);
Assert.IsTrue(test.GetType() == typeof(Nullable)); //FAIL
DateTime? test = new DateTime(434523452345);
Assert.IsTrue(test.GetType() == typeof(Nullable<>)); //STILL FAIL
我的问题是为什么 testInt.GetType() 返回 int,而 typeof(int?) 返回真正的可空类型?
【问题讨论】: