C# 4:动态和可空<>

发布于 2024-09-24 02:10:35 字数 1314 浏览 2 评论 0 原文

因此,我有一些在方法之间传递此匿名对象的代码:

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))
};

接收此匿名对象类型的方法将其类型声明为动态:

private static bool IsPromoActive(dynamic promo)
{
    return /* check StartDate, EndDate */
}

但是,在运行时,如果StartDate code> 或 EndDate 设置为 new Nullable(DateTime.Parse(...)),这是接收此动态的方法对象(名为 promo)执行此操作:

if (promo.StartDate.HasValue && promo.StartDate > DateTime.Today ||
    promo.EndDate.HasValue && promo.EndDate < DateTime.Today)
{
    return;
}

它抛出一个异常:

Server Error in '/' Application.
'System.DateTime' does not contain a definition for 'HasValue' 

这里发生了什么?我对 Nullable 类型和 dynamic 关键字有什么不明白的地方?

在我更改之前,此代码工作正常,我删除了之前存储 TextStartDateEndDatestruct 并替换它具有匿名类型并将其作为动态传递。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-10-01 02:10:35

很好的问题。您可能不知道的两个事实:

  1. 幕后的动态只是对象。也就是说,“动态”变量是一个“对象”变量,向编译器提示“在使用该变量时生成动态操作。”

  2. 不存在装箱可为空这样的东西。当你装箱一个 int 时?要反对,您将获得空对象引用或装箱 int。 int 周围的可为空包装器将被丢弃。

现在应该清楚这里发生了什么。如果促销是动态的,则 promo.StartDate 也是动态的。这意味着在运行时,它是对象。这意味着如果它是值类型,那么它就会被装箱。这意味着如果它可以为空,那么它现在要么是空引用,要么是装箱的不可为空值。不管怎样,那个东西没有 HasValue 属性。如果您想知道它的值类型是否为设置为 null 的可空值类型,请检查 promo.StartDate 是否为 null。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文