c# Dynamic 一种解决方案与另一种解决方案的行为不同

发布于 2024-12-27 23:29:31 字数 1265 浏览 2 评论 0原文

我有一个非常奇怪的问题。如果我创建一个新的控制台应用程序并将代码放入,下面的代码工作正常,但如果我在解决方案中创建一个新的控制台应用程序并粘贴完全相同的代码,我会得到一个运行时绑定程序异常,该动态不包含以下定义你好。奇怪的是,在我现有的解决方案中,代码从未进入 TryGetMember()。

这真的让我很烦恼,解决方案太大,无法迁移到新的解决方案,而且我不相信这会解决它。在不起作用的控制台应用程序中,所有引用都与起作用的控制台应用程序中的相同。唯一的区别是它不在解决方案中。整个解决方案的作用与动力学相同——有趣的是,它正在工作,但突然停止了,所以我创建了这个简单的程序来测试该理论。

编辑:如果我不附加调试器(即 Ctrl+F5),则在解决方案中不起作用的应用程序可以正常工作。

有什么想法吗?

    using System.Collections;
using System.Collections.Generic;
using System.Dynamic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary["hello"] = "world";
            dynamic d = new MyDynamicModel(dictionary);
            var a = d.hello;
        }

    }

    public class MyDynamicModel : DynamicObject
    {
        private IDictionary<string, object> Values { get; set; }
        public MyDynamicModel(IDictionary<string, object> dict)
        {
            Values = dict;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return Values.TryGetValue(binder.Name, out result);
        }
    }
}

I have a really weird problem. The below code works fine if I create a new console app and put the code in but if i create a new console app in my solution and paste exactly the same code in I get a runtime binder exception that the dynamic does not contain a definition for hello. The wierd things is in my existing solution the code never goes into TryGetMember().

This is really bugging me and the solution is too big to move into a new solution and I not convinced that will fix it. In the console application that doesn't work all the reference are the same as in the one that does work. the only difference being it is not in the solution. The whole solution is acting the same way with dynamics - the funny thing is this was working but suddenly stop so i create this simple program to test the theory.

Edit: The application that doesn't work in the solution works fine if I don't attach the debugger i.e. Ctrl+F5.

Any ideas?

    using System.Collections;
using System.Collections.Generic;
using System.Dynamic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary["hello"] = "world";
            dynamic d = new MyDynamicModel(dictionary);
            var a = d.hello;
        }

    }

    public class MyDynamicModel : DynamicObject
    {
        private IDictionary<string, object> Values { get; set; }
        public MyDynamicModel(IDictionary<string, object> dict)
        {
            Values = dict;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return Values.TryGetValue(binder.Name, out result);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2025-01-03 23:29:31

听起来你有视觉工作室设置来打破抛出的异常。仅使用 Dynamics 将引发和处理比处理 C# 对象(包括动态对象)时想象的更多异常。 C# 绑定器总是尝试执行一些操作,例如首先调用静态版本等,然后抛出 RuntimeBindingException,指出它无法找到该成员,处理它,然后再次尝试动态版本。

调试>下异常 确保不会在 RuntimeBinderException 甚至所有 CLR 异常上检查Thrown

It sounds like you have visual studio setup to break on thrown exceptions. Just using Dynamics will throw and handle more exceptions then you think when dealing with C# objects including dynamic objects. The C# binder always tries to do things like call the static version first etc, and then throws a RuntimeBindingException that it can't find the member, handles it, and tries again for the dynamic version.

Under Debug > Exceptions make sure the Thrown isn't checked on RuntimeBinderException or even just all CLR Exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文