[英]C# delegate for two methods with different parameters
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
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. 在2.0中,
Predicate<T>
等就是很好的例子,但你必须拥有相同数量的args。 In this scenario, perhaps an option is to use captures to include the args in the delegate? 在这种情况下,也许一个选项是使用捕获来包含委托中的args?
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 请注意,在.NET 3.5中为您定义了
Action
,但您可以为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
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. 但是你仍然需要使用重载来处理不同数量的参数。
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. 如果查看框架中的
Func<T>
和Action<T>
委托,您将看到它们定义了许多具有不同参数数量的类似委托。 You can use generics, but that doesn't solve the number of arguments issue you're talking about. 您可以使用泛型,但这并不能解决您所讨论的参数问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.