在设计器中打开表单时出现 MissingMethodException;运行时工作正常

发布于 2024-08-20 20:12:07 字数 692 浏览 10 评论 0原文

假设我有一个项目 A,其 A 类具有以下属性:

 公共 bool IsConnected
    {
        获取{返回m_isConnected; }
        私有集 { m_isConnected = value; }
    }

在同一解决方案中,我有一个引用项目 A 的项目 B,并有一个名为 Login 的用户控件。该控件具有以下属性:

 private A m_A = null;

Login 的构造函数中,我执行以下调用:

if (m_A != null && m_A.IsConnected) { ... }

在同一个项目中,主窗体上有一个使用窗体设计器添加的用户控件 A。程序运行良好并且该属性被正确读取。

但是,当在设计器中打开主窗体时,我得到以下执行: MissingMethodException: 'Boolean A.get_IsConnected()'

注释掉 m_A.IsConnected 让我可以使用设计器,但这变得非常烦人。有时它似乎是随机起作用的。

有什么想法吗?

Let's say I have a projoct A with a class A that has this property:

    public bool IsConnected
    {
        get { return m_isConnected; }
        private set { m_isConnected = value; }
    }

In the same solution, I have a project B that references project A and has a user control called Login. This control has this attribute:

    private A m_A = null;

and in the contructor of Login I do this call:

if (m_A != null && m_A.IsConnected) {
... }

In the same project, the main form has on it a user control A that was added with the form designer. The program runs fine and this property is correctly read.

However, when opening the main form in the Designer I get this execption:
MissingMethodException: 'Boolean A.get_IsConnected()'

Commenting out m_A.IsConnected let's me use the designer, but this is getting pretty annoying. And sometimes it seems like it randomly just works.

Any ideas?

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

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

发布评论

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

评论(4

烦人精 2024-08-27 20:12:07

过去有人告诉我 this.DesignMode 并不总是完全可靠。您可以使用的另一个选项是预处理器指令:

#if DESIGN
return;
#else
if (m_A != null && m_A.IsConnected) { /* etc. */ }
#endif

然后添加一个名为 DESIGN 的条件编译符号,您应该会很满意。

I've been told in the past that this.DesignMode isn't always perfectly reliable. The other option you can use is preprocessor directives:

#if DESIGN
return;
#else
if (m_A != null && m_A.IsConnected) { /* etc. */ }
#endif

Then add a conditional compilation symbol named DESIGN and you should be golden.

深陷 2024-08-27 20:12:07

而不是注释掉它

if (this.DesignMode)
{
    return;
}

您可以使用:或

if (!this.DesignMode)
{
    if (m_A != null && m_A.IsConnected) { ... }
}

编辑, :
我记得曾经在单例类上遇到过问题。问题是单例总是被初始化,尽管构造函数从未被调用。如果我正确理解你的问题,你的问题也是同样的。你的成员 m_A 永远不会为 NULL,尽管它应该是。

我通过以下方法解决了我的问题:

在 Visual Studio 中,转到“工具\选项...\调试\常规”并禁用“启用属性评估和其他隐式函数调用”并启用“跳过属性和运算符(仅限托管)”。

也许这有帮助。

Instead of commenting it out you could use:

if (this.DesignMode)
{
    return;
}

or

if (!this.DesignMode)
{
    if (m_A != null && m_A.IsConnected) { ... }
}

Edit:
I remember having problems with a singleton class once. Problem was that the singleton was always initialized although the constructor was never called. If I understand your problem correctly your problem is likewise. Your member m_A is never NULL although it should be.

I managed my problem with the following:

In Visual Studio go to "Tools\Options...\Debugging\General" and disable "Enable property evaluation and other implicit function calls" and enable "Step over properties and operators (Managed only)".

Maybe that helps.

原来分手还会想你 2024-08-27 20:12:07

正如 Ari Roth 所指出的,设计模式无法正常工作。为了弥补这一点,我使用了这种扩展方法:

public static bool IsDesignTime(this Control control)
{
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
    {
        return true;
    }

    if (control.Site != null && control.Site.DesignMode)
    {
        return true;
    }

    var parent = control.Parent;
    while (parent != null)
    {
        if (parent.Site != null && parent.Site.DesignMode)
        {
            return true;
        }
        parent = parent.Parent;
    }
    return false;
}

按照 Simon Linder 描述的相同方式使用它。

As noted by Ari Roth DesignMode doesn't work right. To compensate for this, I use this extension method:

public static bool IsDesignTime(this Control control)
{
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
    {
        return true;
    }

    if (control.Site != null && control.Site.DesignMode)
    {
        return true;
    }

    var parent = control.Parent;
    while (parent != null)
    {
        if (parent.Site != null && parent.Site.DesignMode)
        {
            return true;
        }
        parent = parent.Parent;
    }
    return false;
}

Use it in the same manner that Simon Linder describes.

烟沫凡尘 2024-08-27 20:12:07

看起来设计者使用的是旧版本的控件 DLL,它没有 IsConnected 属性。

MissingMethodException异常与m_A的值无关。 JIT 编译器抱怨该属性丢失。如果将对 m_A.IsConnected 的调用包装在一个方法中,它会起作用,因为不会调用该方法(因为 m_A 实际上为 null),因此不会进行 jitted,因此不需要 IsConnected 属性。

当您将控件放入VS中的窗体中时,设计器会在窗体中创建控件的“真实”实例,该实例会调用构造函数,这会导致JIT编译问题。

It looks like the designer is using an old version of the control DLL, which does not have the IsConnected property.

The MissingMethodException exception is not related to the value of m_A. It is the JIT compiler complaining that the property is missing. If you wrap the call to m_A.IsConnected in a method, it works, because this method is not called (because m_A really is null), thus not jitted, thus the IsConnected property is not needed.

When you put the control in a form in VS, the designer creates a "real" instance of your control in the form, which calls the constructor, which causes the JIT compile problem.

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