【问题标题】:Delegate.CreateDelegate fails when return type is interface当返回类型为接口时,Delegate.CreateDelegate 失败
【发布时间】:2009-05-18 15:36:02
【问题描述】:

我看不到这段代码失败的原因(绑定到目标方法时出错。)

    public interface Interface
    {}

    public class Implementation : Interface
    {}

    public class Program
    {
      public static void Main()
      {
        Invoke();
      }

      public Interface SomeMethod(object arg)
      {
          return new Implementation();
      }

      public void Invoke()
      {
        Delegate someMethod = Delegate.CreateDelegate(typeof(Func<Interface, object>), this, "SomeMethod");
      }
    }

尝试了不同的 CreateDelegate 重载,结果相同:当目标方法返回接口类型时,将委托绑定到方法失败。 任何人都可以对此有所了解吗?

【问题讨论】:

    标签: c# .net delegates


    【解决方案1】:

    您的模板参数是向后的, 它应该是 Func<object,Interface>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多