C#4:动态和可空<> [英] C# 4: Dynamic and Nullable<>

查看:153
本文介绍了C#4:动态和可空<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经得到了各地方法之间该匿名对象传递一些代码:

  VAR促销=新的
{
文本= promo.Value,
起始日期=(的startDate == NULL)?
新可空<&日期时间GT;():
新可空<&日期时间GT;(DateTime.Parse(startDate.Value)),
=结束日期(结束日期== NULL)?
新可空<&日期时间GT;():
新可空<&日期时间GT;(DateTime.Parse(endDate.Value))
};接收这个匿名对象类型



方法声明其类型为动态

 私有静态布尔IsPromoActive(动态促销)
{
返回/ *检查起始日期,结束日期* /
}

在运行时,但是,如果开始日期结束日期设置为新的可空< D​​ateTime的>(DateTime.Parse(...)) ,接收此动态的方法对象(名为促销)执行这样的:

 如果(promo.StartDate.HasValue&安培;&安培; promo.StartDate> DateTime.Today || 
promo.EndDate .HasValue&功放;&安培; promo.EndDate< D​​ateTime.Today)
{
的回报;
}



它抛出一个异常:

 在'/'应用程序的服务器错误。 
'System.DateTime的'不包含'的HasValue'
定义



这是怎么回事这里?有什么不。我了解可空类型和动态关键字?



此代码工作正常之前,我改变了我删除了结构先前存储文本开始日期结束日期和匿名类型代替它,并通过周围为动态


解决方案

大问题。两个事实,你可能不知道的:




    幕后
  1. 动态只是反对。也就是说,一个动态的变量是一个提示编译器,上面写着一个对象变量,当它被用于这个变量产生动态操作。


  2. 有没有这样的事情作为套装空。当你框一个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:

  1. 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."

  2. 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:动态和可空&LT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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