讲解C++与C#里的 回调。
2009-01-13 23:28
190 查看
C++ 有一段时间没用了。先引用一篇文章: http://www.cppblog.com/SpringSnow/archive/2008/12/07/68770.aspx
typedef void (*PF)();
void func()
{
printf("func") ;
}
void caller( PF pf)
{
pf();
}
int _tmain(int argc, _TCHAR* argv[])
{
caller( func ) ;
return 0;
}
至于 C# ,也差不到哪去。只是写法看起来更优雅一些。
namespace ConApp
{
public delegate void func_delegate();
public class Test
{
public event func_delegate FuncEvent; //也可以不定义。 用 Event 定义是其中方式之一。
public void CallerForEvent()
{
FuncEvent();
}
public void CallerForDeleagate(func_delegate CallBack)
{
CallBack();
}
}
/// <summary>
/// Program
/// </summary>
public class Program
{
public void TestCallBack()
{
Console.WriteLine("Hello , In My Program !");
}
/// Main
/// </summary>
/// <param name="args">string[] args)</param>
static void Main(string[] args)
{
// Event 方式调用。
Test test = new Test();
test.FuncEvent += new func_delegate(
delegate()
{
Console.WriteLine(" New Function !") ;
}
);
test.CallerForEvent() ;
// 委托方式调用。
test.CallerForDeleagate(new func_delegate(new Program().TestCallBack));
}
}
}
其目的,都是实现,自己调用了回调函数,具体要做什么,要由回调函数说了算。
typedef void (*PF)();
void func()
{
printf("func") ;
}
void caller( PF pf)
{
pf();
}
int _tmain(int argc, _TCHAR* argv[])
{
caller( func ) ;
return 0;
}
至于 C# ,也差不到哪去。只是写法看起来更优雅一些。
namespace ConApp
{
public delegate void func_delegate();
public class Test
{
public event func_delegate FuncEvent; //也可以不定义。 用 Event 定义是其中方式之一。
public void CallerForEvent()
{
FuncEvent();
}
public void CallerForDeleagate(func_delegate CallBack)
{
CallBack();
}
}
/// <summary>
/// Program
/// </summary>
public class Program
{
public void TestCallBack()
{
Console.WriteLine("Hello , In My Program !");
}
/// Main
/// </summary>
/// <param name="args">string[] args)</param>
static void Main(string[] args)
{
// Event 方式调用。
Test test = new Test();
test.FuncEvent += new func_delegate(
delegate()
{
Console.WriteLine(" New Function !") ;
}
);
test.CallerForEvent() ;
// 委托方式调用。
test.CallerForDeleagate(new func_delegate(new Program().TestCallBack));
}
}
}
其目的,都是实现,自己调用了回调函数,具体要做什么,要由回调函数说了算。
相关文章推荐
- C++动态库中回调C#传入的函数出错
- (转)VS2010 C#调用C++ DLL文件(图文讲解)
- 4000 C#中的回调函数:以最简单的方式讲解最实用的知识点
- C#在C++编写的DLL中注册函数并回调
- C#调用c++的dll 回调使用方法
- C# 调用带有复杂参数的C++ ,delphi dll 注意事项备注。(C++ dll 怎么回调C# 程序,参数怎么写?)
- Unity3d 调用 C++ DLL之 DLL回调Unity3d (C++ DLL回调 C#函数)
- C#与C++回调交互
- c/c++ 回调 c#代码
- C# 调用C++链接库与回调
- C# 使用 MonoPInvokeCallback,让 C 直接回调 C# 函数(C/C++ 跨平台方案)
- (转)VS2010 C#调用C++ DLL文件(图文讲解)
- C++和C#中的回调函数解析
- 详解C#中委托,事件与回调函数讲解
- C#调用C++动态库并回调C#函数
- C++ 编写Dll 回调C#的函数
- C# 调用C/C++ DLL库的回调函数
- C#:请向C++传递全局委托对象以用于回调
- VS2010 C#调用C++ DLL文件(图文讲解)
- 在c++中回调c#函数遇到的字符串问题