复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76using 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方法就是具体的通知了。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21using 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"); } } }
另定义一个被通知的类。可自己定义。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20using 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#仿ios通知(NSNotification)的实现的全部内容,更多相关c#仿ios通知(NSNotification)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复