编写回调的最佳方式是什么?我只需要调用一个sig为void (string,int)的函数;这将需要调用一个类,因为我有需要处理的成员对象。写这篇文章最好的方法是什么?在C中,我会传递一个func指针和一个void*obj。我不喜欢这样,我怀疑在C#中有更好的方法来做到这一点吗?
发布于 2009-04-04 01:32:36
在C#中处理(或取代)回调的标准方法是使用委托或事件。See this tutorial for details.
这提供了一种非常强大、干净的方法来处理回调。
发布于 2009-04-04 01:32:32
C#3.0引入了lambda,允许您放弃对回调(或委托)签名的声明。它允许您执行以下操作:
static void GiveMeTheDate(Action<int, string> action)
{
var now = DateTime.Now;
action(now.Day, now.ToString("MMMM"));
}
GiveMeTheDate((day, month) => Console.WriteLine("Day: {0}, Month: {1}", day, month));
// prints "Day: 3, Month: April"发布于 2009-04-04 01:34:02
这就是你的意思吗?
thatfunc(params, it, wants, Func<myObject> myCallbackFunc)
{
myObject obj = new Object();
myCallbackFunc.Invoke(obj);
//or
myCallbackFunc.Invoke(this);
//I wasn't sure what if myObject contained thatFunc or not...
}https://stackoverflow.com/questions/716274
复制相似问题