在我的主类“a”中,我声明了一个函数和一个委托来调用该函数,我想将我的委托传递给另一个类“b”,但是b类如何知道委托的类型?
甲级
public delegate void SendInfo(string[] info);
SendInfo sendInfo = new SendInfo(SendInformation); //I have a function SendInformation
B b = new B();
b.SetDelegate(sendInfo);
B类
public delegate void SendInfo(string[] info); //I know this is wrong but how will
SendInfo SendInformation; //this class know what SendInfo is?
public void SetDelegate(SendInfo sendinfo) //What type is this parameter?
{
sendinfo.GetType();
SendInformation = sendinfo;
}
谢谢,
埃蒙
最佳答案
当您在类A中声明委托时,您将其声明为类A的子类型。例如,它的类型是ClassA.SendInfo
。在B班你可以用
public void SetDelegate(ClassA.SendInfo sendinfo)
或者,在类A的代码之外声明委托-然后它将只是另一种类型,您可以通过名称引用(
SendInfo
)。