System.Dynamic错误?错误、System、Dynamic

2023-09-03 16:13:25 作者:凉生

虽然我玩的C#4.0的动态,我发现了奇怪的事情发生与code是这样的:

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);
    }
}

好了,它的工作原理,但...看看的IntelliTrace窗口:

Ok, it works but... take a look at IntelliTrace window:

所以每invokation(和其他业务也对动态物体)会导致抛出和捕获异常诡异两次!

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.

但是...的code以上的行为看起来很奇怪。也许抛出和捕获异常每秒两次任何操作DynamicObject - 是一个错误

But... behavior of the code above looks very strange. Maybe throwing and catching exceptions twice per any operation on DynamicObject - is a bug?

推荐答案

谢谢,我已经开了一个错误,我们正在寻找它。我会更新这个曾经我从编译器团队听到。它扔在C#运行时绑定(Microsoft.CSharp.dll)。

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).

如果您启用了第一次机会异常的Debug.Exceptions,你会打这个。的IntelliTrace无关的错误,它只是显示你的第一次机会异常被抛出和吞咽。

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.