c#仿ios通知(NSNotification)的實現

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Notificationcenter
{
    public enum Event
    {
        Event_load,
        Event_add,
        Event_remove
    }
    interface Notification
    {
        ArrayList objlist { get; set; }

        void removeobjformlist(myclass obj, string strmethod, Event e);
        void addobjtolist(myclass obj,string strmethod, Event e);

        void update(Event e);
       
    }
    public class callbackserver : Notification
    {
        public callbackserver() {
            objlist = new ArrayList();
        }
        public ArrayList objlist{get;set; }

        public void addobjtolist(myclass obj, string strmethod, Event e)
        {
            string objname = obj.GetType().FullName;
           
            Console.WriteLine(""+objname);
            Dictionary<string,object> map = new Dictionary<string,object>();
            map.Add("obj",objname);
            map.Add("method", strmethod);
            map.Add("evt", e);
            objlist.Add(map);
        }

        public void removeobjformlist(myclass obj, string strmethod, Event e)
        {
            for (int i = 0; i < objlist.Count; i++) {
                Dictionary<string, object> map = (Dictionary<string, object>)objlist[i];
                if (((string)map["obj"] == obj.GetType().FullName) && ((Event)map["evt"] == e) && ((string)map["method"] == strmethod)) {
                    objlist.Remove(obj);
                    Console.WriteLine("remove  ok");
                    break;
                };
            }
            
        }

        public void update(Event e)
        {
            foreach (object o in objlist)
            {
                Dictionary<string, object> map = (Dictionary < string, object>) o;
                if ((Event)map["evt"] == e)
                {
                    Object obj = getobjformobjname((string)map["obj"]);
                    MethodInfo method = getmethodformobj(obj, (string)map["method"]);
                    method.Invoke(obj, null);
                }
        
            }
        }
        public Object getobjformobjname(string strobj)
        {
            Console.WriteLine("" + strobj);
            Type type = Type.GetType(strobj);
            return System.Activator.CreateInstance(type);
         }
        public MethodInfo getmethodformobj(Object obj, string strmethod)
        {

            return obj.GetType().GetMethod(strmethod, new Type[] { });
            
        }
    }

}

 先是通知類,定義事件Evnet,定義arraylist存放被通知對象。再定義註冊通知和移除通知方法(addobjtolist和removeobjformlist),update方法就是具體的通知了。ide

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notificationcenter
{
    public abstract class myclass {
        public abstract void on_Event_load();
        public void on_Event_add() { }
        public void on_Event_remove() { }
    }
    class submack : myclass
    {

        public override void on_Event_load()
        {
            Console.WriteLine("wo is loading");
        }

       
    }

}

另定義一個被通知的類。可本身定義。測試

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notificationcenter
{
    class Program
    {
        static void Main(string[] args)
        {
            Notification notification = new callbackserver();
            submack sub = new submack();
            notification.addobjtolist(sub, "on_Event_load", Event.Event_load);
            notification.update(Event.Event_load);
            Console.ReadKey();
        }
    }
}

測試類。new 通知類對象,new 被通知對象,註冊通知,而後是發生通知事件。 本例用到觀察者模式,和反射。spa

相關文章
相關標籤/搜索