如何在C#/.NET 4.0中编写带有委托参数的方法?

如何在C#/.NET 4.0中编写带有委托参数的方法?,c#,.net,delegates,C#,.net,Delegates,我一直在使用在类级别声明委托的方式: protected delegate void FieldsDelegate(); //and then write a method e.g. protected int CreateComponent(DbConnection cnctn, string tableName, Dictionary<string, object> changedFieldValues, FieldsDelegate fieldsDelegate) 受保护

我一直在使用在类级别声明委托的方式:

protected delegate void FieldsDelegate();

//and then write a method e.g.

protected int CreateComponent(DbConnection cnctn, string tableName, Dictionary<string, object> changedFieldValues, FieldsDelegate fieldsDelegate)
受保护的委托无效字段delegate();
//然后写一个方法,例如。
受保护的int-CreateComponent(数据库连接cnctn、字符串表名、字典更改字段值、字段删除字段删除字段删除)
然而,这真的很麻烦,我无法立即看到代理是什么样子。所以我想这样做:

protected int CreateComponent(DbConnection cnctn, string tableName, Dictionary<string, object> changedFieldValues, delegate void fieldsDelegate())
受保护的int-CreateComponent(数据库连接cnctn、字符串表名、字典更改字段值、委托无效字段delegate())
所以我没有单独的定义

由于某些原因,上述情况是不允许的。那么我该怎么做呢?

.NET现在为此提供了和泛型

在您的情况下,此委托不接受任何参数,也不返回任何内容

// protected delegate void FieldsDelegate(); // Don't need this anymore

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues, 
                               Action fieldsDelegate
                            )
//受保护的委托无效字段delegate();//我不再需要这个了
受保护的int-CreateComponent(
数据库连接cnctn,
字符串表名,
字典更改字段值,
行动现场特使
)
如果它将字符串作为参数:

// protected delegate void FieldsDelegate(string s); 

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues,
                               Action<string> fieldsDelegate
                             )
//受保护的委托无效字段delegate(字符串s);
受保护的int-CreateComponent(
数据库连接cnctn,
字符串表名,
字典更改字段值,
行动现场特使
)
如果它将字符串作为参数并返回布尔值:

// protected delegate bool FieldsDelegate(string s); 

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues,
                               Func<string, bool> fieldsDelegate
                             )
//受保护的委托bool FieldsDelegate(字符串s);
受保护的int-CreateComponent(
数据库连接cnctn,
字符串表名,
字典更改字段值,
Func fieldsDelegate
)

您可以使用通用的
操作
函数
及其变体作为委托,好处是您甚至不需要定义单独的委托

Action
最多占用16个不同的类型参数,因此:
Action
等等;每个类型参数都是位于相同位置的方法的类型参数。因此,
操作
适用于此方法:

public void MyMethod(int number, string info)
Func
与之相同,只是它适用于返回值的方法。最后一个类型参数是返回类型
Func
不是您在本例中使用的

示例:
Func
适用于以下方法:

public object MyOtherMethod(string message, int number)
使用这些泛型委托可以清楚地说明该委托参数的参数是什么,这似乎是您的意图

public void MyMethod(Action<string, MyClass>, string message)

在这里,您知道需要传递一个方法,该方法接受一个
int
参数,并返回
MyOtherClass

实际上
FieldsDelegate
中的后缀
Delegate
已经是一个很好的指示器了。@Henk-我同意。如果我将其替换的代理包括在内,则更容易理解。
public void MeOtherMethod(Func<int, MyOtherClass>, int iterations)