简体   繁体   English

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

[英]how to write a method with delegate parameter in C# / .NET 4.0?

I've been using the way that I declare delegate in class level:我一直在使用在 class 级别声明委托的方式:

protected delegate void FieldsDelegate();

//and then write a method e.g.

protected int CreateComponent(DbConnection cnctn, string tableName, Dictionary<string, object> changedFieldValues, FieldsDelegate fieldsDelegate)

However this is really cumbersome and I can't see instantly what the delegate is like.然而,这真的很麻烦,我无法立即看到代表是什么样的。 So I would like to do it like this:所以我想这样做:

protected int CreateComponent(DbConnection cnctn, string tableName, Dictionary<string, object> changedFieldValues, delegate void fieldsDelegate())

so that I don't have the separate definition.所以我没有单独的定义。

The above is not allowed for some reason.由于某种原因,上述内容是不允许的。 So how can I do it?那么我该怎么做呢?

.NET now provides the Action and Func generics for this purpose. .NET 现在为此提供了ActionFunc generics。

In your case, this delegate takes no parameters and returns nothing.在您的情况下,此委托不接受任何参数并且不返回任何内容。

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

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues, 
                               Action fieldsDelegate
                            )

If it took a string as a parameter:如果将字符串作为参数:

// protected delegate void FieldsDelegate(string s); 

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues,
                               Action<string> fieldsDelegate
                             )

If it took a string as a parameter and returned a bool:如果它接受一个字符串作为参数并返回一个布尔值:

// protected delegate bool FieldsDelegate(string s); 

protected int CreateComponent(
                               DbConnection cnctn, 
                               string tableName, 
                               Dictionary<string, object> changedFieldValues,
                               Func<string, bool> fieldsDelegate
                             )

You could use the generic Action<T> and Func<T> and their variants as delegates, and the bonus is you don't even need to define a separate delegate at all.您可以使用通用的Action<T>Func<T>及其变体作为委托,而且您甚至根本不需要定义单独的委托。

Action<T> takes up to 16 different type parameters, so: Action<T1, T2> and on up; Action<T>最多需要 16 个不同的类型参数,因此: Action<T1, T2>及以上; Each type parameter is the type parameter for the method in the same position.每个类型参数是同一个 position 中方法的类型参数。 So, Action<int, string> would work for this method:因此, Action<int, string>将适用于此方法:

public void MyMethod(int number, string info)

Func<T> is the same, except it is for methods that return a value. Func<T>是相同的,除了它用于返回值的方法。 The last type argument is the return type.最后一个类型参数是返回类型。 Func<T> is not what you would use in your case here. Func<T>不是您在此处使用的情况。

Example: Func<string, int, object> would be for a method such as:示例: Func<string, int, object>将用于以下方法:

public object MyOtherMethod(string message, int number)

Using these generic delegates makes it clear what the arguments for that delegate argument would be, which seems to be your intent.使用这些通用委托可以清楚地表明该委托参数的 arguments 是什么,这似乎是您的意图。

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

Calling that method, you know you need to pass a method in that takes a string and a MyClass .调用该方法,您知道您需要传递一个采用stringMyClass的方法。

public void MeOtherMethod(Func<int, MyOtherClass>, int iterations)

Here, you know you need to pass a method that takes an int parameter, and returns MyOtherClass在这里,您知道您需要传递一个采用int参数并返回MyOtherClass的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM