System.Dynamic bug?

Posted by ControlFlow on Stack Overflow See other posts from Stack Overflow or by ControlFlow
Published on 2010-03-12T07:54:20Z Indexed on 2010/03/12 7:57 UTC
Read the original article Hit count: 408

Filed under:
|
|
|
|

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:

screenshot

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#4.0