相关疑难解决方法(0)

作为委托的构造函数 - 它可能在C#中吗?

我有一个类如下:

class Foo
{
  public Foo(int x) { ... }
}
Run Code Online (Sandbox Code Playgroud)

我需要传递一个像这样的委托的方法:

delegate Foo FooGenerator(int x);
Run Code Online (Sandbox Code Playgroud)

是否可以直接将构造函数作为FooGenerator值传递,而无需键入:

delegate(int x) { return new Foo(x); }
Run Code Online (Sandbox Code Playgroud)

编辑:对于我个人使用,问题涉及.NET 2.0,但欢迎3.0 +的提示/响应.

c# constructor delegates

57
推荐指数
4
解决办法
2万
查看次数

委托任何方法类型 - C#

我想要一个将执行任何外部方法的类,如下所示:

class CrazyClass
{
  //other stuff

  public AnyReturnType Execute(AnyKindOfMethod Method, object[] ParametersForMethod)
  {
    //more stuff
    return Method(ParametersForMethod) //or something like that
  }
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?是否有代理人采用任何方法签名?

c# generics reflection delegates method-signature

16
推荐指数
1
解决办法
3162
查看次数