It seems like you overly complicate things. Both the function and its argument are known when you add them to the list. You considered using anonymous functions. As an example, I wrapped this object .. the string argument in this example. DynamicInvoke will also be significantly slower.
Also, two different types can return the same GetHashCode , which, depending on your specific needs, may or may not matter.
public partial class IBaseEvent { private Dictionary<int, Action> funcs = new Dictionary<int, Action>(); public void Execute() { foreach (var func in funcs.Values) { func(); } } public void AddFunction(Type t, Action ff) { funcs.Add(t.GetHashCode(), ff); } } public class DummyEvent : IBaseEvent { private string EventType = "DUMMY_EVENT"; private void DoSomething(string x) { Console.WriteLine(x); } public DummyEvent() { Action temp = () => { DoSomething("Hello World from DummyEvent! TypeCode"); }; AddFunction(typeof(Logging), temp); } }
If the type is not strictly needed, you can just like that
public partial class IBaseEvent { public Action MyAction; public void Execute() { MyAction(); } public void AddFunction(Action ff) { MyAction += ff; } }
source share