返回类型为interface时,Delegate.CreateDelegate失败

时间:2009-05-18 15:36:03

标签: c# .net delegates

我无法看到此代码失败的原因(错误绑定到目标方法。)

    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的不同重载:当目标方法返回接口类型时,将委托绑定到方法失败。 任何人都可以对此有所了解吗?

1 个答案:

答案 0 :(得分:7)

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