使用 Func Delegates 在类之间发送消息
我可以尝试使用 func 委托将消息发送到另一个类。我通常与常规代表一起执行此操作,但我正在尝试学习如何使用 Func 执行此操作。在下面的示例中,我希望 AClass 用消息更新主类。正如你所看到的,我遇到了麻烦。
更新:我更新了示例以提供更好的细节。在示例中,我展示了如何使用委托与主类进行通信。是否可以使用 func 委托来做到这一点?
class Program
{
static void Main(string[] args)
{
AClass aClass = new AClass();
// old way
aClass.MessageEvent += new AClass.MessageEventHandler(ProcessMessage);
aClass.MessageSender(50);
// new way
Func<int, String> fDelegate = aClass.MessageSender;
Console.WriteLine(fDelegate(10));
Console.ReadLine();
}
public static void ProcessMessage(String mess) {
Console.WriteLine("Process message here: " + mess);
}
}
class AClass {
public event MessageEventHandler MessageEvent;
public delegate void MessageEventHandler(String value);
public AClass() {
}
public String MessageSender(int i) {
InvokeMessage("Message: " + i);
return "Message: " + i;
}
public void InvokeMessage(String mess) {
this.MessageEvent("Message sent via delegate: " + mess);
}
}
输出:
Process message here: Message sent via delegate: Message: 50
Process message here: Message sent via delegate: Message: 10
Message: 10
如有任何建议,我们将不胜感激。谢谢
I can trying to use a func delegate to send messages to another class. I normally do this with a regular delegate but I am trying to learn how to do this with Func. With the below example, I want AClass to update the main class with messages. As you can see, I am having trouble.
Updated: I updated the example to provide better detail. In the example I show how I use a delegate to communicate to the main class. Is it possible to do this using the func delegate?
class Program
{
static void Main(string[] args)
{
AClass aClass = new AClass();
// old way
aClass.MessageEvent += new AClass.MessageEventHandler(ProcessMessage);
aClass.MessageSender(50);
// new way
Func<int, String> fDelegate = aClass.MessageSender;
Console.WriteLine(fDelegate(10));
Console.ReadLine();
}
public static void ProcessMessage(String mess) {
Console.WriteLine("Process message here: " + mess);
}
}
class AClass {
public event MessageEventHandler MessageEvent;
public delegate void MessageEventHandler(String value);
public AClass() {
}
public String MessageSender(int i) {
InvokeMessage("Message: " + i);
return "Message: " + i;
}
public void InvokeMessage(String mess) {
this.MessageEvent("Message sent via delegate: " + mess);
}
}
Output:
Process message here: Message sent via delegate: Message: 50
Process message here: Message sent via delegate: Message: 10
Message: 10
Any advice is appreciated. thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我明白你想要实现的目标。据我了解,您希望使用委托来处理 AClass 中主类中的数据。即使使用通用委托,也有很多方法可以做到这一点,但这里有一个示例:
这与您之前的事件委托相匹配,因为它不需要输出参数。这是 Action<'Tin> 和 Action<'Tin> 之间的主要区别。和Func<'Tin,Tout>。如果您希望主类方法将数据返回给委托调用者,则需要使用 Func。如果我遗漏了您的要求中的某些内容,请告诉我。
I think i understand what you are attempting to accomplish. From what i understand you want to used a delegate to process data in the main class from the AClass. There are lots of ways to do this even with the generic delegates but here is one example:
This matches your previous event delegation as it did not require an output param. This is the main difference between Action<'Tin> and Func<'Tin, Tout>. If you want your main class method to return data back to the delegate caller is when you would use Func. If i have missed something in your requirements please let me know.