C#4:动态和可空<> [英] C# 4: Dynamic and Nullable<>
问题描述
所以,我已经得到了各地方法之间该匿名对象传递一些代码:
VAR促销=新的
{
文本= promo.Value,
起始日期=(的startDate == NULL)?
新可空<&日期时间GT;():
新可空<&日期时间GT;(DateTime.Parse(startDate.Value)),
=结束日期(结束日期== NULL)?
新可空<&日期时间GT;():
新可空<&日期时间GT;(DateTime.Parse(endDate.Value))
};接收这个匿名对象类型
方法声明其类型为动态
:
私有静态布尔IsPromoActive(动态促销)
{
返回/ *检查起始日期,结束日期* /
}
在运行时,但是,如果开始日期
或结束日期
设置为新的可空< DateTime的>(DateTime.Parse(...))
,接收此动态的方法
对象(名为促销
)执行这样的:
如果(promo.StartDate.HasValue&安培;&安培; promo.StartDate> DateTime.Today ||
promo.EndDate .HasValue&功放;&安培; promo.EndDate< DateTime.Today)
{
的回报;
}
它抛出一个异常:
在'/'应用程序的服务器错误。
'System.DateTime的'不包含'的HasValue'
定义
这是怎么回事这里?有什么不。我了解可空
类型和动态
关键字?
此代码工作正常之前,我改变了我删除了结构
先前存储文本
,开始日期
和结束日期
和匿名类型代替它,并通过周围为动态
。
大问题。两个事实,你可能不知道的:
-
动态只是反对。也就是说,一个动态的变量是一个提示编译器,上面写着一个对象变量,当它被用于这个变量产生动态操作。
-
有没有这样的事情作为套装空。当你框一个int?反对你使用一个null对象引用或装箱的int。周围的INT可空包装被扔掉。
幕后
现在应该清楚是怎么回事。如果促销是动态的,那么promo.StartDate是动态的。这意味着在运行时,它是对象。这意味着,如果它是值类型,它是盒装。这意味着,如果是空的,现在是不是空引用或盒装不可为空值。无论哪种方式,该事不具有HasValue属性。如果你想知道它是否在其值类型形成一个空值类型设置为空,然后检查是否promo.StartDate为空或不是。</ p>
So I've got some code that passes around this anonymous object between methods:
var promo = new
{
Text = promo.Value,
StartDate = (startDate == null) ?
new Nullable<DateTime>() :
new Nullable<DateTime>(DateTime.Parse(startDate.Value)),
EndDate = (endDate == null) ?
new Nullable<DateTime>() :
new Nullable<DateTime>(DateTime.Parse(endDate.Value))
};
Methods that receive this anonymous object type declare its type as dynamic
:
private static bool IsPromoActive(dynamic promo)
{
return /* check StartDate, EndDate */
}
At run-time, however, if StartDate
or EndDate
are set to new Nullable<DateTime>(DateTime.Parse(...))
, a method that receives this dynamic
object (named promo
) performs this:
if (promo.StartDate.HasValue && promo.StartDate > DateTime.Today ||
promo.EndDate.HasValue && promo.EndDate < DateTime.Today)
{
return;
}
It throws an exception:
Server Error in '/' Application.
'System.DateTime' does not contain a definition for 'HasValue'
What's going on here? What don't I understand about Nullable
types and the dynamic
keyword?
This code worked fine before I changed I removed the struct
that previously stored Text
, StartDate
, and EndDate
and replaced it with an anonymous type and passed it around as dynamic
.
Great question. Two facts that you probably don't know:
Dynamic behind the scenes is just object. That is, a "dynamic" variable is an "object" variable with a hint to the compiler that says "generate dynamic operations on this variable when it is used."
There is no such thing as a boxed nullable. When you box an int? to object you get either a null object reference or a boxed int. The nullable wrapper around the int is thrown away.
Now it should be clear what is going on here. If promo is dynamic then promo.StartDate is dynamic. Which means that at runtime, it is object. Which means that if it is of value type, it is boxed. Which means that if it was nullable, it is now either a null reference or a boxed non-nullable value. Either way, that thing doesn't have a HasValue property. If you want to know whether it was in its value type form a nullable value type set to null, then check whether promo.StartDate is null or not.
这篇关于C#4:动态和可空<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!