【发布时间】:2013-11-24 23:45:58
【问题描述】:
我的问题和这些问题有点相似:
replay a list of functions and parameters
C# delegate for two methods with different parameters
我的目标是将函数调用及其参数存储在一个列表中,以便在我的管理器类安排的不同线程中调用它们。
- 调用函数时,将自身添加到记住参数和值的函数列表中
- 当函数结束时,我想取回返回对象(如果有的话)
- 允许稍后调用函数列表
- 有不同的方法,具有完全不同的签名 (有的有返回值(bool, int, object..),有的没有,方法参数个数不固定)
例如,我想这样调用函数:
ServerManager.addDoSomething(ServerManager.SERVICES.Login, serverURL, userName, password); // Login() with bool return type and 3 string parameters
ServerManager.addDoSomething(ServerManager.SERVICES.Query, searchExpr); // Query() with MyData return type and 1 string parameters
ServerManager.addDoSomething(ServerManager.SERVICES.Modify, searchExpr, newVal); // Modify() with int return type and 2 string parameters
ServerManager.addDoSomething(ServerManager.SERVICES.Logout); // Logout() with void return type and 0 parameters
或类似:
ServerManager.addDoSomething(() => ServerManager.SERVICES.Query (searchExpr));
ServerManager.addDoSomething(() => ServerManager.SERVICES.Modify (searchExpr, newVal)); ServerManager.addDoSomething(() => ServerManager.SERVICES.Logout()); ServerManager.addDoSomething(() => ServerManager.SERVICES.Login(serverURL, userName, password));
或其他支持接口的方式..
如果我想支持延迟函数调用,我的 ServerManager.addDoSomething 方法(或不同签名的方法)应该是什么样子,我应该使用什么数据结构(WHAT_SHOULD_I_STORE)。我怎样才能取回我的返回值?
我认为,我不能以这种方式使委托通用,我可以用它来存储具有不同签名的方法..
public static void addDoSomething(Delegate delegateParameter, string ...);
or
public static void addDoSomething(Func<...> methodToCall, string ...);
or
public static void addDoSomething(Action methodToCall, string ...);
or
public static void addDoSomething(delegate methodToCall, string ...);
我的课:
public class ServerManager
{
static List< WHAT_SHOULD_I_STORE > requestFIFO = new List< WHAT_SHOULD_I_STORE >();
public static IServerConnection SERVICES ;
static BackgroundWorker worker = new BackgroundWorker();
public ServerManager()
{
SERVICES = new ServerConnection();
worker.DoWork += (o, ea) =>
{
try
{
WHAT_SHOULD_I_STORE mr = null;
Application.Current.Dispatcher.Invoke(new Action(() => mr = popQueueElement() ));
if (mr != null)
processRequestFromQueue(mr);
}
catch (Exception)
{
}
};
worker.RunWorkerCompleted += (o, ea) =>
{
worker.RunWorkerAsync();
};
if ( ! worker.IsBusy ) worker.RunWorkerAsync();
}
private WHAT_SHOULD_I_STORE popQueueElement()
{
if (requestFIFO != null && requestFIFO.Count > 0)
{
WHAT_SHOULD_I_STORE result = requestFIFO.ElementAt(0);
requestFIFO.Remove(result);
return result;
}
else
return null;
}
private addDoSomething(...)
{
//....
}
}
public class ServerConnection : IServerConnection
{
// Concrete implementations of the IServerManager interface
}
public interface IServerConnection
{
bool Login (string serverURL, string userName, string password);
MyData Query (string serverURL, searchExpr);
int Modify (string searchExpr, string newVal);
void Logout ();
// ...
}
【问题讨论】:
-
更新:很抱歉,我错过了名为“worker”的变量是 BackgroundWorker 类的一个实例。
标签: c# delegates action func delayed-execution