C# 在方法中创建委托类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/359320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:26:59  来源:igfitidea点击:

Creating a delegate type inside a method

c#.net-2.0delegates

提问by Nikola Stjelja

I want to create a delegate type in C# inside a method for the purpose of creating Anonymous methods.

我想在 C# 中的一个方法中创建一个委托类型,以创建匿名方法。

For example:

例如:

public void MyMethod(){
   delegate int Sum(int a, int b);

   Sum mySumImplementation=delegate (int a, int b) {return a+b;}

   Console.WriteLine(mySumImplementation(1,1).ToString());
}

Unfortunately, I cannot do it using .NET 2.0 and C# 2.0.

不幸的是,我不能使用 .NET 2.0 和 C# 2.0 来做到这一点。

采纳答案by Jon Skeet

Why do you want to create the delegate type within the method? What's wrong with declaring it outside the method? Basically, you can't do this - you can't declare a type(any kind of type) within a method.

为什么要在方法中创建委托类型?在方法之外声明它有什么问题?基本上,您不能这样做 - 您不能在方法中声明类型(任何类型)。

One alternative would be to declare all the Func/Action generic delegates which are present in .NET 3.5 - then you could just do:

一种替代方法是声明 .NET 3.5 中存在的所有 Func/Action 通用委托 - 然后你可以这样做:

public void MyMethod(){
    Func<int, int, int> mySumImplementation = 
        delegate (int a, int b) { return a+b; };

    Console.WriteLine(mySumImplementation(1,1).ToString());
}

The declarations are on my C#/.NET Versions page.

声明在我的C#/.NET 版本页面上

回答by jalf

The delegate typehas to be defined outside the function. The actual delegate can be created inside the method as you do.

委托类型必须在函数外部定义。可以像您一样在方法内部创建实际的委托。

class MyClass {
  delegate int Sum(int a, int b);
  public void MyMethod(){

       Sum mySumImplementation=delegate (int a, int b) {return a+b;}

       Console.WriteLine(mySumImplementation(1,1).ToString());
  }

}

would be valid. The best solution may be to emulate .NET3.5, and create some generic delegate types globally, which can be used all over your solution, to avoid having to constantly redeclare delegate types for everything:

将是有效的。最好的解决方案可能是模拟 .NET3.5,并在全局范围内创建一些通用委托类型,它们可以在您的整个解决方案中使用,以避免必须不断地为所有内容重新声明委托类型:

delegate R Func<R>();
delegate R Func<T, R>(T t);
delegate R Func<T0, T1, R>(T0 t0, T1 t1);
delegate R Func<T0, T1, T2, R>(T0 t0, T1 t1, T2 t2);

Then you can just use a Func<int, int, int>delegate in your code above.

然后您可以Func<int, int, int>在上面的代码中使用委托。

回答by Cheng Chen

Delegates are compiled to classes(a class that inherits from System.MulticastDelegate). In C#, you are not allowed to declare a class inside a method(see C# language specification). Thus you can't declare a delegate in a method, either.

委托被编译为类(从 System.MulticastDelegate 继承的类)。在 C# 中,不允许在方法中声明类(请参阅 C# 语言规范)。因此,您也不能在方法中声明委托。

回答by Dim_Ka

What about this:

那这个呢:

static void Main(string[] args)
{
    Expression<Func<int, int, int>> exFunc = (a, b) => a + b;
    var lambda = exFunc as LambdaExpression;
    Delegate del = exFunc.Compile();
    Console.WriteLine(del.DynamicInvoke(2, 2));
}