I would like to invoke a lambda expression dynamically, but all I got is the methodInfo to do so.
Something like:
Magic.RegisterStaticPacketHandler<TestPacket>((a, b) => { /* Do Stuff */ }); Magic class: public void RegisterStaticPacketHandler<T>(PacketReceivedHandler<T> handler) where T : Packet { //Remember the handler with: handler.GetMethodInfo() } And later on I would like to invoke this method. Because PacketReceivedHandler is a delegate, I can get the "MethodInfo" out of it. If I invoke this MethodInfo with just:
methodInfo.Invoke(obj, new object[] { packet, this }); I ofc receive an exception, that the given object (obj) doesn't fit. Setting BindingFlags like "NonPublic | Instance | Static" doesnt help either.
UPDATE
PacketReceivedHandler looks as follows:
public delegate void PacketReceivedHandler<T>(T packet, Connection connection) where T : Packet; And I save it in my Magic class:
private Dictionary<int, Tuple<MethodInfo, object>> id_methodInfo_object = new Dictionary<int, Tuple<MethodInfo, object>>(); - MethodInfo = The delegate
- object = Where the delegate comes from
- id = UID for networking stuff
Action<T>be easier?RegisterStaticPacketHandler<T, U>(Action<T, U> handler)and then just dohander([instance of T], [instance of U])Targetof the delegate