举个用自定义事件调用其他类中的private函数的简单例子吧.
在class A中有private的函数sayHello.那class B中肯定不能直接调用sayHello了.当然我们另外还要假设在A中用到了B.
delegate void DelegateSayHello(string name); //在类外面某个地方,它其实也可以看成一种特殊的类了.当然也可以放在某个类里面定义
public class A
{
B sb;
sb.HowAreYou += new DelegateSayHello(sayHello);
private void sayHello(string name)
{
Console.WriteLine("Hello," + name);
}
}
public class B
{
public event DelegateSayHello HowAreYou;
string name = "arwen";
private void DoSomething()
{
HowAreYou(name); //这里就调用了class A中的private函数sayHello了啊
}
}