Skip to content

Instantly share code, notes, and snippets.

@preslavrachev
Created March 19, 2012 07:25
Show Gist options
  • Save preslavrachev/2100747 to your computer and use it in GitHub Desktop.
Save preslavrachev/2100747 to your computer and use it in GitHub Desktop.
public static class EventRegister
{
public static Dictionary<String, Dictionary<int,Action<object>>> Map = new Dictionary<string,Dictionary<int,Action<object>>>();
}
/* 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);
}
}
}
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");
}
}
@preslavrachev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment