美文网首页
Attribute预定义特性及其应用

Attribute预定义特性及其应用

作者: LEO_青蛙 | 来源:发表于2020-03-31 11:37 被阅读0次
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
    1、AttributeTargets  //成员属性
    AttributeTargets.All  //指示属性可以应用到所有程序元素
    AttributeTargets.Class  //指示属性只可以应用于某个类
    AttributeTargets.Method  //指示属性只可以应用于某个方法
    2、Inherited  //继承属性
    3、AllowMultiple  //指示元素中是否可存在属性的多个实例
    

    应用实例:

    //Subscribe预定义特性
    [AttributeUsage(AttributeTargets.Method)]
    public class Subscribe : Attribute
    {
        protected object m_Type { set; get; }
        public Subscribe(object type)
        {
            m_Type = type;
        }
        public Enum GetSubscription()
        {
            return (Enum)m_Type;
        }
    }
    
    //Notification通知对象
    public class Notification
    {
        public Enum Type { set; get; }
        public object[] Params { set; get; }
        public object Target { set; get; }
        public Notification()
        {
        }
    }
    
    //Notifier通知者
    public class Notifier
    {
        object m_Target = null;
        Dictionary<Enum, Action<Notification>> m_DictAction = new Dictionary<Enum, Action<Notification>>();
        public Notifier()
        {
            m_Target = this;
            Init();
        }
        public Notifier(object target)
        {
            m_Target = target;
            Init();
        }
        void Init()
        {
            MethodInfo[] methods = m_Target.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            for (int i = 0; i < methods.Length; ++i)
            {
                MethodInfo m = methods[i];
                Subscribe sub = m.GetCustomAttribute<Subscribe>();
                if (sub != null)
                    Add(sub.GetSubscription(), m.CreateDelegate(typeof(Action<Notification>), m_Target) as Action<Notification>);
            }
        }
        public void Add(Enum type, Action<Notification> receiver)
        {
            if(!m_DictAction.ContainsKey(type))
            {
                m_DictAction.Add(type, receiver);
                _Add(type, this);
            }
        }
        public void Remove(Enum type)
        {
            m_DictAction.Remove(type);
            _Remove(type, this);
        }
        public void Send(Enum type, params object[] datas)
        {
            Notification note = new Notification();
            note.Type = type;
            note.Params = datas;
            note.Target = m_Target;
            _Send(type, note);
        }
        void _Execute(Enum type, Notification note)
        {
            if(m_DictAction.ContainsKey(type))
                if(m_DictAction[type] != null)
                    m_DictAction[type](note);
        }
        #region  static functions
        static readonly Dictionary<Enum, List<Notifier>> s_DictNotifiers = new Dictionary<Enum, List<Notifier>>();
        static void _Add(Enum type, Notifier notifier)
        {
            if(!s_DictNotifiers.ContainsKey(type))
                s_DictNotifiers.Add(type, new List<Notifier>());
            if(!s_DictNotifiers[type].Contains(notifier))
                s_DictNotifiers[type].Add(notifier);
        }
        static void _Remove(Enum type, Notifier notifier)
        {
            if(s_DictNotifiers.ContainsKey(type))
            {
                if(s_DictNotifiers[type].Contains(notifier))
                    s_DictNotifiers[type].Remove(notifier);
            }
        }
        static void _Send(Enum type, Notification note)
        {
            if(s_DictNotifiers.ContainsKey(type))
            {
                List<Notifier> notifiers = s_DictNotifiers[type];
                int count = notifiers.Count;
                for(int i=count-1; i>=0; --i)
                {
                    if(notifiers[i] != null)
                        notifiers[i]._Execute(type, note);
                }
            }
        }
        #endregion
    }
    
    //继承Notifier
    public class ClientService : Notifier
    {
        //使用Subscribe预定义特性
        [Subscribe(S2CMessageId.ResponseClientConnected)]
        void OnResponseClientConnected(Notification note)
        {
        }
        //使用Subscribe预定义特性
        [Subscribe(S2CMessageId.ResponseEnterRoom)]
        void OnResponseEnterRoom(Notification note)
        {
        }
    }
    //继承Notifier
    public class ServerService : Notifier
    {
        //使用Subscribe预定义特性
        [Subscribe(C2SMessageId.RequestEnterRoom)]
        void OnRequestEnterRoom(Notification note)
        {
        }
    }
    

    相关文章

      网友评论

          本文标题:Attribute预定义特性及其应用

          本文链接:https://www.haomeiwen.com/subject/oewiuhtx.html