可变数量的参数 [英] Variable number of parameters
问题描述
我正在改进我的项目的现有消息传递系统,它应该接受可变数量的参数。现在,泛型被用来将参数传递给系统。很多代码重复,所以问题是 - 是否可以将具有不同参数数量的类的所有版本合并到单个类中?
我以此为基础的整个邮件系统可以在这里找到: CSharpMessenger
I'm improving an existing messaging system for my project, which should accept a variable number of parameters. Right now generics are used to pass arguments to the system. A lot of code repeats itself, so the question is - is it possible to merge all the versions of the class that take different number of parameters into a one single class? The whole messaging system I take as a foundation for mine can be found here: CSharpMessenger
代码摘录:
public delegate void Callback();
public delegate void Callback<T>(T arg1);
public delegate void Callback<T, U>(T arg1, U arg2);
没有参数的版本:
static public class Messenger
{
private static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
static public void AddListener(string eventType, Callback handler)
{
if (!eventTable.ContainsKey(eventType))
{
eventTable.Add(eventType, null);
}
// Add the handler to the event.
eventTable[eventType] = (Callback)eventTable[eventType] + handler;
}
static public void Invoke(string eventType)
{
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback callback = (Callback) d;
if (callback != null)
{
callback();
}
}
}
}
版本有一个参数:
static public class Messenger<T>
{
private static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
static public void AddListener(string eventType, Callback<T> handler)
{
if (!eventTable.ContainsKey(eventType))
{
eventTable.Add(eventType, null);
}
// Add the handler to the event.
eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
}
static public void Invoke(string eventType, T arg1)
{
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T> callback = (Callback<T>) d;
if (callback != null)
{
callback(arg1);
}
}
}
}
版本有两个参数:
static public class Messenger<T, U>
{
private static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
static public void AddListener(string eventType, Callback<T, U> handler)
{
if (!eventTable.ContainsKey(eventType))
{
eventTable.Add(eventType, null);
}
// Add the handler to the event.
eventTable[eventType] = (Callback<T, U>)eventTable[eventType] + handler;
}
static public void Invoke(string eventType, T arg1, U arg2)
{
Delegate d;
if (eventTable.TryGetValue(eventType, out d))
{
Callback<T, U> callback = (Callback<T, U>) d;
if (callback != null)
{
callback(arg1, arg2);
}
}
}
}
As你可以看到大部分的代码本身都是重复的,是否可以创建同一个类的一般通用版本,这将采用可变数量的参数,并避免多次重复相同的代码?
As you can see most of the code repeats itself, Is it possible to create some general version of the same class, that will take a variable number of parameters and to avoid repeating the same code so many times?
谢谢。
推荐答案
回调参数通常传递给 EventArgs的子类
。已经有一个简单的泛型委托来抽象出来: EventHandler< TEventArgs>其中TEventArgs:EventArgs
。参数的变化可以添加到 EventArgs
的子类中。
Callback parameters are normally passed in a subclass of EventArgs
. There's already a simple generic delegate that abstracts this: EventHandler<TEventArgs> where TEventArgs : EventArgs
. The variability in parameters can be added to subclasses of EventArgs
.
所以,如果你可以重构代码来使用这些课程,你可以只有一个版本。这样的东西:
So, if you can restructure your code to use these classes, you can have just one version. Something like this:
public static class Messenger<TEventArgs> where TEventArgs : EventArgs {
private static Dictionary<string, EventHandler<TEventArgs>> eventTable =
new Dictionary<string, EventHandler<TEventArgs>>();
public static void AddListener(string eventType, EventHandler<TEventArgs> handler) {
if (eventTable.ContainsKey(eventType)) {
eventTable[eventType] = eventTable[eventType] + handler;
}
else {
eventTable.Add(eventType, handler);
}
}
public static void Invoke(string eventType, TEventArgs args) {
EventHandler<TEventArgs> d;
if (eventTable.TryGetValue(eventType, out d)) {
if (d != null) {
d(args);
}
}
}
}
这篇关于可变数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!