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 map = new Dictionary();
            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 map = (Dictionary)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 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方法就是具体的通知了。

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 被通知对象,注册通知,然后是发生通知事件。 本例用到观察者模式,和反射。

转载于:https://my.oschina.net/u/989459/blog/1836796

你可能感兴趣的:(c#)