Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Dynamic bug?

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 http://img717.imageshack.us/img717/4914/10435230.png

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?

like image 894
controlflow Avatar asked Mar 12 '10 07:03

controlflow


2 Answers

Thanks, I've opened a bug, we're looking at it. I'll update this once I hear from the Compiler team. It's throwing in the C# runtime binder (Microsoft.CSharp.dll).

If you enable first-chance exceptions in Debug.Exceptions, you will hit this. IntelliTrace has nothing to do with the bug, it's just showing you the first-chance exception being thrown and swallowed.

like image 146
Kirill Osenkov Avatar answered Sep 22 '22 03:09

Kirill Osenkov


I think the exceptions are caused by the debugger trying too inspect something.

If you tell Visual Studio to stop whenever a exception are thrown it does not stop and this indicates that the debugger is responsible for the exceptions not the actual code.

like image 37
Arve Avatar answered Sep 24 '22 03:09

Arve