0
我想了解下面的代码。委托与参数定义但用作参数没有自己的参数? c#
它有一个功能创建与代表CallbackRcvData作为参数。 委托是用参数定义的,但代码中的函数只是将委托作为参数表示,但该参数/委托没有自己定义中的必需参数。这个构造奇妙地起作用。我不明白...我是代表新人。如果您需要更多信息,请告诉我们
函数OpenConnection来自dll。
public class myClass
{
public delegate int CallbackRcvData(UInt32 ConnectionId, IntPtr Data, Int32 size, Int32 Status);
public void Create(string TargetName, string Login, string Password, Int32 Protocol,UInt32 PortCOM, CallbackRcvData Callback)
{
int result = 0;
result = OpenConnection(TargetName, ref this.ConnectionId, Login, Password, 0, Protocol, 0, 0, Callback,0);
if (result != 0)
{
throw new System.ArgumentException("Parameter cannot be null", "original");
}
}
}
public class callingClass
{
...in some Method
ptrFct = new myClass.CallbackRcvData(this.RcvData);
public int RcvData(UInt32 ConnectionId,IntPtr Data, Int32 size, Int32 Status)
{
if ((size > 0) && (Status == 0) && (Data != null))
{
this.Displaytext(Marshal.PtrToStringAnsi(Data));
}
return 0;
}
public void ThreadProc()
{
try
{
//open putty session , portCOM not used in this example not serial link
Session.Create(Settings.DeviceAdd, Settings.Login, Settings.Password, Settings.protocol, 0, ptrFct);
this.Connection_End(1);
}
catch
{
this.Connection_End(0);
}
}
}
抱歉,错误代码格式不正确。我发现在这里使用格式选项非常困难。
我想我明白了。委托* Callback *可以被理解为指向函数的指针,它被引用到* OpenConnection *函数中,该函数又用参数调用引用的函数。 – Simorgh