存储/传递代表作为变量 [英] storing/passing delegates as variables
问题描述
我是相当新的C#和正在制定一个办法,实行动态GUI至极采用串行通信。我最初来自C,所以函数指针的概念很熟悉。
Basicly我希望在serial命令已被处理调用一个answerFunction()函数。
在理论:
我有一个类lbl_txtBox_Pair这是动态地在运行时创建的。
我有一个类comObject它与串行端口进行通信。
我有第三类comPacket持有关于一个串行指令的所有信息。
- 在类lbl_txtBox_Pair的对象我实例化一个数据包,并告诉它该功能时,串口命令完成应该被调用。
- 我给包对象的实例comObject。
- 后beeing处理comObject希望通过调用存储在包对象的代表信号的数据包的原始发送者。
- in an Object of Class lbl_txtBox_Pair I instanciate a Packet and tell it which function should be called when the serial command is finished.
- I give the packet Object to the comObject Instance.
- after beeing processed the comObject wants to signal the original sender of the packet by calling the delegate which is stored in the Packet Object.
醇>
有关某种原因,我无法得到它的工作。它告诉我,包的属性不调用。 ?我做得不对teribly
下面是代码:
第一类中的代码lbl_txtBox_Pair。我在这里创建comPacket,并把它交给comObject
公共委托无效answerHandler(comPacket包);
公共无效txb_value_KeyPress(对象发件人,KeyPressEventArgs E)
{
如果(e.KeyChar ==(char)的Keys.Return)
{
answerHandler answerMethod =新answerHandler(this.processAnswer);
comPacket问题=新comPacket(this.command,answerMethod,1);
comObject.addPacket(问题);
}
}
comPacket的构造。这里委托被存储稍后调用
公共委托answerFunction;
公共comPacket(字符串CMD,代表FUNC,INT PRIO)
{
this.cmd = CMD;
answerFunction = FUNC;
this.prio = PRIO;
}
在comObject数据包得到处理。完成后我要调用存储在数据包的功能。该comObject运行在的方式在不同的线程。
如果(this.isEndtocken(inputline))
$ { b $ b听= FALSE;
packet.answerFunction(包);
}
和这里是被它打破。 packet.answerFunction(包);不会执行并表示它不能被称为方法。
任何人能看到它出错?我觉得好像委托失去,这是一个代表什么的信息。 ;)
或我必须完全地重组代码中使用其他类型的回调/事件方法
更改 comPacket
来采取一个强类型的委托:
公共answerHandler answerFunction ;
公共comPacket(字符串CMD,answerHandler FUNC,INT PRIO)
{
this.cmd = CMD;
answerFunction = FUNC;
this.prio = PRIO;
}
如果你仍想保留委托引用弱类型,您可以利用 DynamicInvoke
而不是: http://msdn.microsoft.com/en-us/library/system.delegate.dynamicinvoke.aspx
编辑:另一种选择,如果你想维持强类型的代表尚未有不同的用途是利用仿制药。您的委托可以被安置在一个通用类,并针对泛型类型配合其签名。
I am fairly new to C# and was working on a a way to implement a dynamic GUI wich uses the serial communication. I originally come from C, so the concept of function pointer is familiar.
Basicly I want to invoke a answerFunction() function when the serial command has been processed. In Theory: I have a Class lbl_txtBox_Pair which is dynamicly created on runtime. I have a Class comObject which communicates with the serial Port. I have a third class comPacket which holds all information regarding one serial command.
For some reason I can't get it to work. It tells me that the Attribute of Packet is not callable. Am I doing something teribly wrong?
Here is the Code: first the code in Class "lbl_txtBox_Pair". I create the comPacket here and give it to the comObject.
public delegate void answerHandler( comPacket packet);
public void txb_value_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
answerHandler answerMethod = new answerHandler(this.processAnswer);
comPacket question = new comPacket(this.command, answerMethod, 1);
comObject.addPacket(question);
}
}
The constructor of comPacket. Here the delegate gets stored to be called later.
public Delegate answerFunction;
public comPacket(string cmd, Delegate func, int prio)
{
this.cmd = cmd;
answerFunction = func;
this.prio = prio;
}
In the comObject the Packets get processed. When finished I want to call the function stored in the Packet. The comObject runs in a different Thread by the way.
if (this.isEndtocken(inputline))
{
listen = false;
packet.answerFunction(packet);
}
And here it is were it breaks. packet.answerFunction(packet); wont execute and says it cant be called as Method.
Can anybody see where it goes wrong? I think it seems like the delegate looses the information that it is a delegate or something. ;) Or do I have to completly restructure the code to use other types of callback / Event Methods?
Change your comPacket
to take a strongly typed delegate:
public answerHandler answerFunction;
public comPacket(string cmd, answerHandler func, int prio)
{
this.cmd = cmd;
answerFunction = func;
this.prio = prio;
}
If you still want to keep the delegate reference weakly typed, you can leverage DynamicInvoke
instead: http://msdn.microsoft.com/en-us/library/system.delegate.dynamicinvoke.aspx
EDIT: Another option if you want to maintain strongly typed delegates yet have different usages is to leverage generics. Your delegate can be housed in a generic class and tie its signature against that generic type.
这篇关于存储/传递代表作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!