- 类库中的内容: // 新建—-项目—–C#类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace event_dll
{
public class Class1
{
public delegate void event_write(int da);
public event event_write event_write_call;
//异步事件函数的调用为 Class1.event_write_call+= delegate( int e){};
public void write_date()
{
Console.WriteLine("function write_date is called ");
event_write_call(100); // 触发异步事件,相当于C语言中的回调函数
}
}
}
- 调用类:
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using event_dll;
namespace event_callback
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("main function ");
Class1 event_call_obj = new Class1() ;
event_call_obj.event_write_call += delegate(int e)
// 相当于C语言中 回调函数内容为 如下
{
Console.WriteLine("e val is "+ e);// 当在另一个类中调用声明的委托(即指针函数),将触发
};
event_call_obj.write_date();//执行write_date()内容,并触发回调函数
}
}
}
C# static的应用注意:
C#的static 除了隐藏外,有些不同于嵌入式C,如下列
Static 声明的变量已经驻留在内存中了。
对 ip 、clientSocket 的方位 ,通过
Net2Opreation Net2_Operation = new Net2Opreation() ;
Net2_Operation.ip 或者 Net2_Operation.clientSocket 的方式访问
对于 public static event Net2AsyncRcv HandleNet2AsyncRcv;的访问是如下:
Net2Opreation.HandleNet2AsyncRcv += delegate(byte data) ;
// 注意Net2_Operation 与Net2Opreation 的不通
public class Net2Opreation
{
public IPAddress ip = null;
public Socket clientSocket = null;
public static byte[] receive_buffer = new byte[100];
public delegate void Net2AsyncRcv(byte data);
public static event Net2AsyncRcv HandleNet2AsyncRcv;
public static byte di_val;
public static byte do_val;
public void Net2Init(string ipstring) // port 1278
{
}