具有不同参数的两种方法的 C# 委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/377217/
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
C# delegate for two methods with different parameters
提问by alexandrul
I am using the following methods:
我正在使用以下方法:
public void M1(Int32 a)
{
// acquire MyMutex
DoSomething(a);
// release MyMutex
}
and
和
public void M2(String s, String t)
{
// acquire MyMutex
DoSomethingElse(s, t);
// release MyMutex
}
From what I have found so far it seems that it is not possible to use a single delegate for two methods with different signatures.
从我目前发现的情况来看,似乎不可能对具有不同签名的两种方法使用单个委托。
Are there any other alternatives to write something like this:
有没有其他替代方法可以写这样的东西:
public void UsingMutex(...)
{
// acquire MyMutex
...
// release MyMutex
}
UsingMutex(M1);
UsingMutex(M2);
All I can think for the moment is to use two delegates and a boolean flag to know which delegate to call, but it is not a long term solution.
目前我能想到的就是使用两个委托和一个布尔标志来知道要调用哪个委托,但这不是一个长期的解决方案。
It is possible to combine generics with delegates? And if so, do you have some links for any kind of documentation?
可以将泛型与委托结合使用吗?如果是这样,您是否有任何类型文档的链接?
Environment: C# 2.0
环境:C# 2.0
采纳答案by Marc Gravell
Absolutely you can mix delegates with generics. In 2.0, Predicate<T>
etc are good examples of this, but you must have the same number of args. In this scenario, perhaps an option is to use captures to include the args in the delegate?
绝对可以将委托与泛型混合使用。在 2.0 中,Predicate<T>
等等都是很好的例子,但你必须有相同数量的 args。在这种情况下,也许一个选项是使用捕获将 args 包含在委托中?
i.e.
IE
public delegate void Action();
static void Main()
{
DoStuff(delegate {Foo(5);});
DoStuff(delegate {Bar("abc","def");});
}
static void DoStuff(Action action)
{
action();
}
static void Foo(int i)
{
Console.WriteLine(i);
}
static void Bar(string s, string t)
{
Console.WriteLine(s+t);
}
Note that Action
is defined for you in .NET 3.5, but you can re-declare it for 2.0 purposes ;-p
请注意,这Action
是在 .NET 3.5 中为您定义的,但您可以为 2.0 目的重新声明它;-p
Note that the anonymous method (delegate {...}
) can also be parameterised:
请注意,匿名方法 ( delegate {...}
) 也可以参数化:
static void Main()
{
DoStuff(delegate (string s) {Foo(5);});
DoStuff(delegate (string s) {Bar(s,"def");});
}
static void DoStuff(Action<string> action)
{
action("abc");
}
static void Foo(int i)
{
Console.WriteLine(i);
}
static void Bar(string s, string t)
{
Console.WriteLine(s+t);
}
Finally, C# 3.0 makes this all a lot easier and prettier with "lambdas", but that is another topic ;-p
最后,C# 3.0 使用“lambdas”使这一切变得更容易和更漂亮,但这是另一个主题;-p
回答by Brian Rasmussen
If you look at the Func<T>
and Action<T>
delegates in the framework, you'll see that they define a number of similar delegates with different number of parameters. You can use generics, but that doesn't solve the number of arguments issue you're talking about.
如果您查看框架中的Func<T>
和Action<T>
委托,您会发现它们定义了许多具有不同数量参数的类似委托。您可以使用泛型,但这并不能解决您正在谈论的参数数量问题。
回答by Mehrdad Afshari
Yes, it's possible to combine generics with delegates.
是的,可以将泛型与委托结合使用。
public delegate void Action<T>(T x);
public delegate void Action<T,U>(T x, U y);
public void UsingMutex<T>(Action<T> x, T t) {
// acquire mutex...
x(t);
// release mutex...
}
public void UsingMutex<T,U>(Action<T,U> x, T t, U u) {
// acquire mutex...
x(t, u);
// release mutex...
}
But you still have to handle different number of parameters using overloads.
但是您仍然必须使用重载处理不同数量的参数。