views:

171

answers:

1

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.

+19  A: 

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.

Eric Lippert
Very nice answer. Certainly enlightened me.
Mike Hofer
So you're saying the runtime treats `Nullable<T>` special when dealing with `dynamic`?
strager
@strager: No, that's not at all what I'm saying. The runtime knows *nothing* about "dynamic". "dynamic" exists only at compile time. *The runtime treats Nullable<T> as special when executing a boxing instruction.*
Eric Lippert
Would it make sense for the C# runtime binder to treat "HasValue" method calls specially when applied to a null reference to avoid this situation?
kvb
@kvb: No, that doesn't make sense because the suggestion does not avoid the stated situation at all. *The reference isn't null.* The error reported is that a boxed DateTime does not have a member called HasValue, not that someone is calling HasValue on a null reference.
Eric Lippert
Good point - you could try to patch that up as well by having "HasValue" return true when the argument is a boxed instance of a value type, but perhaps it's better not to go down that (increasingly complicated) road...
kvb