I’ve build a program that uses the Dynamic keyword.
at a point in my code I do this:
public void OnNext(ExpandoObject value)
{
dynamic expando = value;
if (expando.Attention == NotifyEnums.ALERT)
{
_needsAttention = true;
}
}
And this works, so I submit it to SourceControl. Then my boss gets the files, tries to run it but gets an error on line if (expando.Attention == NotifyEnums.ALERT), apparently expando.Attention does not exist in the dynamic object:

This confused me greatly, because we both target the same .NET version: .NET Framework 4 Platform Update 1 KB2478063 and I KNOW the value is set in the code.
So I set a breakpoint before the dynamic value is read, and open up the Immediate Window.
expando.Attention // Gives an exception on boss computer, works on my computer
But look at the following:
(((IDictionary<String, object>)expando).ContainsKey("Attention"))
true // Returns "True" on boss computer and on my computer, WTF!
So I try the following:
(NotifyEnums)(((IDictionary<String, Object>)expando)["Attention"])
ALERT // Returns alert on boss computer
So to summarize:
public void OnNext(ExpandoObject value)
{
dynamic expando = value;
if (expando.Attention == NotifyEnums.ALERT)
// CRASHES on boss pc, works on my pc
// Error says Attention does not exist.
{
_needsAttention = true;
}
}
AND
public void OnNext(ExpandoObject value)
{
dynamic expando = value;
if ((NotifyEnums)(((IDictionary<String, Object>)expando)["Attention"]) == NotifyEnums.ALERT)
// WORKS on BOSS PC (wtf?) and works on my pc.
{
_needsAttention = true;
}
}
So what is going on, can anyone elaborate?
Edit:
But there is another thing as well, after the Program Crahses, I click Continue, get the error again, click continue again and then the program continues as if nothing happened. It reads the correct value out of the dynamic object.
It looks like you have first chance exceptions turned on the boss machine. Look under Debug/Exceptions and uncheck everything in the Thrown column.
It is normal for the expando object to throw an exception when the class doesn’t implement the member directly, it uses the exception to fire a secondary lookup and return the correct information.