While I playing with the C# 4.0 dynamic, I found strange things happening with the code like this:
using System.Dynamic;
sealed class Foo : DynamicObject
{
public override bool TryInvoke(
InvokeBinder binder, object[] args, out object result)
{
result = new object();
return true;
}
static void Main()
{
dynamic foo = new Foo();
var t1 = foo(0);
var t2 = foo(0);
var t3 = foo(0);
var t4 = foo(0);
var t5 = foo(0);
}
}
Ok, it works but... take a look at IntelliTrace window:
So every invokation (and other operations too on dynamic object) causes throwing and catching strange exceptions twice!
I understand, that sometimes exceptions mechanism may be used for optimizations, for example first call to dynamic may be performed to some stub delegate, that simply throws exception - this may be like a signal to dynamic binder to resolve an correct member and re-point delegate. Next call to the same delegate will be performed without any checks.
But... behavior of the code above looks very strange. Maybe throwing and catching exceptions twice per any operation on DynamicObject - is a bug?