请教大牛们event关键字的问题
请教大牛们
public event SerialDataReceivedEventHandler DataReceived;
和
public SerialDataReceivedEventHandler DataReceived;
的区别. Public?Event
[解决办法]
不加event的话,你怎么给这个事件指定处理函数呀,例如你的类叫MyClass,如果有event的话,你可以这样写
MyClass mc=new MyClass();
mc.DataReceived+=MyClass_DataReceived;
public void MyClass_DataReceived()
{
}
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var x = new TA();
x.Alert = process1;
x.Alert += process2;
x.Alert += process3;
x.callMethod();
Console.WriteLine("-----------------------");
var m = (MulticastDelegate)x.Alert;
x.Alert = (TA.EvtHandler)Delegate.Combine(m.GetInvocationList().Where((p, i) => i % 2 == 0).ToArray()); //只留下奇数个回调
x.callMethod();
Console.WriteLine("-----------------------");
x.Alert = process3; //彻底重建回调
x.callMethod();
Console.ReadKey();
}
private static void process1()
{
Console.WriteLine("process1");
}
private static void process2()
{
Console.WriteLine("process2");
}
private static void process3()
{
Console.WriteLine("process3");
}
}
public class TA
{
public delegate void EvtHandler();
public EvtHandler Alert;
public void callMethod()
{
if (Alert != null)
Alert();
}
}
}