Created
March 19, 2012 07:25
-
-
Save preslavrachev/2100747 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EventRegister | |
{ | |
public static Dictionary<String, Dictionary<int,Action<object>>> Map = new Dictionary<string,Dictionary<int,Action<object>>>(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* we are using the assumption that every entity is constructed recursively as every child component is given an id of +1 | |
so it could be that my id is #5, but if I have 7 child components, the ID of my next neighbor (the next entity constructed by my parent, which is on the same tree level ) will be #13, because I will be constructed recursively to the lowest level first */ | |
public static void sendMessage(String methodName, Object args) | |
{ | |
int id = 5; | |
int parentId = 0; | |
int nextNeighborId = 14; | |
Dictionary<int, Action<Object>> d = EventRegister.Map[methodName]; | |
foreach (KeyValuePair<int, Action<Object>> kv in d) | |
{ | |
if (kv.Key >= id && kv.Key < nextNeighborId) | |
{ | |
kv.Value(args); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Handler : System.Attribute | |
{ | |
} | |
class TestClass | |
{ | |
int id = 12; | |
int parentId = 5; | |
int parentNextNeighbourId = 14; | |
public TestClass() | |
{ | |
foreach (MethodInfo mi in this.GetType().GetMethods()) | |
{ | |
foreach (System.Attribute attr in mi.GetCustomAttributes(false)) | |
{ | |
if (attr is Handler) | |
{ | |
Action<object> converted = (Action<object>)Delegate.CreateDelegate(typeof(Action<object>), this, mi); | |
if (!EventRegister.Map.ContainsKey(mi.Name)) | |
{ | |
//EventRegister.Map[mi.Name].Add(parentId, this.demo); | |
EventRegister.Map.Add(mi.Name, new Dictionary<int, Action<object>>()); | |
EventRegister.Map[mi.Name].Add(parentId, converted); | |
} | |
else | |
{ | |
EventRegister.Map[mi.Name][parentId] += converted; | |
} | |
} | |
} | |
} | |
} | |
[Handler] | |
public void demo(Object arg) | |
{ | |
Console.Out.WriteLine("I was hit"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx