退订代表通过ref关键字传递给认购方式? [英] Unsubscribe from delegate passed through ref keyword to the subscription method?

查看:116
本文介绍了退订代表通过ref关键字传递给认购方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

 公共类终端:IDisposable的
{
只读清单< IListener> _听众;

公共端(IEnumerable的< IListener>听众)
{
_listeners =新的List< IListener>(听众);
}

公共无效订阅(REF动作<串GT;源)
{
源+ =广播;
//某种方式存储的参考?
}

无效广播(字符串消息)
{
的foreach(在_listeners VAR监听器)listener.Listen(消息);
}

公共无效的Dispose()
{
//退订所有存储的来源是什么?
}
}



我搜索了一会儿,似乎与ref关键字传递参数不能存储。试图源参数添加到列表或将其分配到一个字段变量不允许它来保持实际委托的原件备查参考;所以我的问题是:




  • 有没有办法从所有来源退订,而无需再次通过他们引用

  • 如果没有,怎么可以在类,以便通过传递一个委托通过的方法?

  • 来支持它,但仍维持认购改变是否有可能实现的,而不使用反射?

  • 是否有可能实现它没有一个类包装委托/事件,然后传递类作为认购?

  • 参数< 。/ ul>

    感谢您



    编辑:看来,不使用包装或反射,有没有解决给定的问题。我的目的是使类尽可能多的便携成为可能,而无需辅助类包装的代表。感谢大家的贡献


    解决方案

    修改:好吧,这是个坏主意,做回最基础的:



    我推荐了一个Action创建一个包装类:

     类ActionWrapper 
    {
    公共动作<串GT;行动;
    }

    和调整您最初的类包装工作:

     私人ActionWrapper localSource; 

    公共无效订阅(ActionWrapper源)
    {
    source.Action + =广播;
    localSource =来源;
    }

    公共无效的Dispose()
    {
    localSource.Action - =广播;
    }

    现在,你应该得到想要的结果。


    I've got the following class:

    public class Terminal : IDisposable
    {
        readonly List<IListener> _listeners;
    
        public Terminal(IEnumerable<IListener> listeners)
        {
            _listeners = new List<IListener>(listeners);
        }
    
        public void Subscribe(ref Action<string> source)
        {
            source += Broadcast;
            //Store the reference somehow?
        }
    
        void Broadcast(string message)
        {
            foreach (var listener in _listeners) listener.Listen(message);
        }
    
        public void Dispose()
        {
            //Unsubscribe from all the stored sources?
        }
    }
    

    I've searched for a while and it appears that an argument passed with the ref keyword can't be stored. Trying to add the source argument to a list or to assign it to a field variable doesn't allow it to keep a reference to the actual delegate's original reference; so my questions are:

    • Is there a way to unsubscribe from all the sources without passing their references again?
    • If not, how can the class be changed in order to support it, but still maintain the subscription by passing a delegate through a method?
    • Is it possible to achieve it without using Reflection?
    • Is it possible to achieve it without wrapping the delegate/event in a class and then passing the class as a parameter for the subscription?

    Thank you.

    EDIT: It appears that without using a Wrapper or Reflection, there's no solution to the given problem. My intention was to make the class as much portable as possible, without having to wrap delegates in helper classes. Thanks everyone for the contributions.

    解决方案

    Edit: Ok, that was a bad idea, so back to the basics:

    I recommend creating a wrapper class over an Action:

    class ActionWrapper
    {
        public Action<string> Action;
    }
    

    And restructuring your initial class to work with wrappers:

    private ActionWrapper localSource;
    
    public void Subscribe(ActionWrapper source)
    {
        source.Action += Broadcast;
        localSource = source;        
    }
    
    public void Dispose()
    {
        localSource.Action -= Broadcast;
    }
    

    Now you should get the desired results.

    这篇关于退订代表通过ref关键字传递给认购方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆