使用 Func Delegates 在类之间发送消息

发布于 2024-12-11 03:18:14 字数 1384 浏览 0 评论 0原文

我可以尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-12-18 03:18:14

我想我明白你想要实现的目标。据我了解,您希望使用委托来处理 AClass 中主类中的数据。即使使用通用委托,也有很多方法可以做到这一点,但这里有一个示例:

    static void Main(string[] args)
    {
        AClass aClass = new AClass();
        aClass.MessageDelegate = ProcessMessage;
        // Send Message (writing of output unnecessary as it happens via the delegate anyway)
        Console.WriteLine(aClass.MessageSender(50));

        Console.ReadLine();
    }

    /// <summary>
    /// Used as delegate (messages passed from AClass to here)
    /// </summary>
    /// <param name="mess"></param>
    public static void ProcessMessage(String mess)
    {
        Console.WriteLine("Process message here: " + mess);
    }

    class AClass
    {
        public Action<string> MessageDelegate
        { get; set; }

        public AClass()
        {
        }

        public String MessageSender(int i)
        {
            InvokeMessage("Message: " + i);
            return "Message: " + i;
        }

        public void InvokeMessage(String mess)
        {
            if (MessageDelegate != null)
            {
                MessageDelegate(mess);
            }                
        }
    }

这与您之前的事件委托相匹配,因为它不需要输出参数。这是 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:

    static void Main(string[] args)
    {
        AClass aClass = new AClass();
        aClass.MessageDelegate = ProcessMessage;
        // Send Message (writing of output unnecessary as it happens via the delegate anyway)
        Console.WriteLine(aClass.MessageSender(50));

        Console.ReadLine();
    }

    /// <summary>
    /// Used as delegate (messages passed from AClass to here)
    /// </summary>
    /// <param name="mess"></param>
    public static void ProcessMessage(String mess)
    {
        Console.WriteLine("Process message here: " + mess);
    }

    class AClass
    {
        public Action<string> MessageDelegate
        { get; set; }

        public AClass()
        {
        }

        public String MessageSender(int i)
        {
            InvokeMessage("Message: " + i);
            return "Message: " + i;
        }

        public void InvokeMessage(String mess)
        {
            if (MessageDelegate != null)
            {
                MessageDelegate(mess);
            }                
        }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文