【问题标题】:No overload for '<method>' matches delegate '<delegate>''<method>' 没有重载匹配委托 '<delegate>'
【发布时间】:2011-06-05 00:06:05
【问题描述】:

我的“安全”程序集包含以下代码:

    public delegate void InteropEventDelegate(InteropEventType etype, string data, string data2, string data3);
    public event InteropEventDelegate InteropEvent;

第二个程序集引用了我的“安全”程序集,并包含以下代码:

    void LoadSecurity()
    {
        if (!AssemblyIsLocked && Security == null)
        {
            this.Security = new Security.Security(UnlockCode);
            this.Security.InteropEvent += new Security.Security.InteropEventDelegate(Security_InteropEvent);
        }
    }

    void Security_InteropEvent(InteropEventType etype, string data, string data2, string data3)
    {
        throw new NotImplementedException();
    }

Security_InteropEvent 由 IntelliSense 生成,并且具有正确的签名,但我收到错误消息“'Security_InteropEvent' 没有重载与委托 'Security.Security.InteropEventDelegate' 匹配”。为什么?

【问题讨论】:

    标签: c# visual-studio-2010 .net-3.5


    【解决方案1】:

    您是否在某处声明了另一种名为InteropEventType 的类型?这将使Security_InteropEvent 的第一个参数与InteropEventDelegate 的第一个参数的类型不同。

    虽然我提到了名称,但我强烈建议您不要为类型和命名空间赋予相同的名称。 Eric Lippert 有一个whole blog series 来讨论这个危险。 (我说的是Security.Security,我最初认为它是一个命名不佳的命名空间,直到我看到你在它上面调用了一个构造函数。)

    【讨论】:

    • 我支持这个建议。简单摘录Security = new Security.Security同一个词有三种意思!
    • 是的,我在程序集之间共享了一个 ENUMS.CS 文件,其中定义了 InteropEventType。即使它在两个地方的定义相同,我想它也被视为两种不同的类型。有没有办法在程序集之间共享一个枚举,以便像我所拥有的那样工作?
    • @PahJoker:基本上,您通过使一个程序集引用另一个程序集来“共享”枚举。永远不要在两个不同的程序集中定义一个类型——这必然会导致问题。
    猜你喜欢
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多