C# 重写WndProc时Visual Studio designer崩溃

C# 重写WndProc时Visual Studio designer崩溃,c#,winforms,C#,Winforms,我有一个从System.Windows.Forms.control继承的控件,该控件重写了WndProc函数以将消息传递到外部DLL。函数看起来像 public class GraphicsPanel : Control { bool designMode; EngineWrapper Engine; public GraphicsPanel() { designMode = (LicenseManager

我有一个从System.Windows.Forms.control继承的控件,该控件重写了WndProc函数以将消息传递到外部DLL。函数看起来像

public class GraphicsPanel : Control
{
        bool designMode;
        EngineWrapper Engine;

        public GraphicsPanel()
        {
            designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
            this.SetStyle(ControlStyles.Selectable, true);
            this.TabStop = true;
        }

        public void SetEngine(EngineWrapper Engine)
        {
            this.Engine = Engine;
        }
    
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if(designMode) // Tried this option
                return;

            if (!designMode && Engine != null) // And this option
                Engine.ProcessWindowMessage(m.Msg, m.WParam, m.LParam);
        }
}
如果我在这个函数中提到引擎,它会崩溃,因为在显示设计器时,外部DLL显然没有加载。我收到错误消息“无法加载文件或程序集…”或其依赖项之一

我当然应该补充一点,这段代码都是在运行时工作的,而不是在设计时。每当我想使用设计器时,就必须注释掉两行引擎代码,这很烦人

关于如何让设计器在包含这行代码的同时正确工作的任何建议


您是否需要在设计时执行WndProc? 如果不是,您可以尝试向If添加条件:

bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
if (!designMode && Engine != null)
    ...
您将需要导入System.ComponentModel命名空间


我通过将引擎引用移动到另一个函数来解决这个问题。这很奇怪,但它可以工作

    void PassMessage(Message m)
    {
        if (Engine != null)
            Engine.ProcessWindowMessage(m.Msg, m.WParam, m.LParam);
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        if (!designMode)
            PassMessage(m);
    }

有没有编译器指令可以做同样的事情?这当然是一个很好的提示,但它并没有解决设计器错误。我不这么认为,请检查。你是说
designMode
在设计时是错误的吗?你能提供关于错误的更多信息吗?designMode是正确的,但因为变量引擎引用了它其他未加载的dll无法为设计器进行编译。
引擎如何定义/创建?它是对dll中定义的对象的引用。它是在运行时创建的,因此进行空检查。