1

I have a function declared along the lines of:

public delegate void Callback<T>(T arg1) where T : EventT;
public void RegisterCallback<T>(Callback<T> callback) where T : EventT

I want to call this via reflection as the type in a particular instance isn't know to me at runtime, I'm trying

Type classType = Type.GetType (eventNameString);
MethodInfo baseMethod = eventDispatcher.GetType ().GetMethod ("RegisterCallback");
MethodInfo typedMethod = baseMethod.MakeGenericMethod(new Type[] {classType});

Which if I understand it correctly is now making a method to except this dynamic type, however my concern is how to pass the Callback parameter into the method invoke

typedMethod.Invoke (eventDispatcher, new object[] { ??? });

I'm not sure how I can declare a delegate to pass into the object[] params that will also be typed agaisnt classType, I'm fairly new to Csharp as well so am a little lost,

Thanks in advance

  • Sounds like you have functionally the same problem described in [this question here](http://stackoverflow.com/questions/12865848/general-purpose-fromevent-method). – Servy Jan 13 '15 at 18:56

1 Answers1

0

simply pass your instance of you delegate

static void myglobalcallback(object arg)
{
    //....
}

var dmethod = new DynamicMethod(string.Empty, typeof(void), new Type[] { thegenerictype }, true);
var body = dmethod.GetILGenerator();
body.Emit(opcodes.ldarg_0);
body.Emit(opcodes.castclass, typeof(object));
body.emit(opcodes.call, typeof(mycurrenttype).getmethod("myglobalcallback"));
body.emit(opcodes.ret);
var delegate = dmethod.createdelegate(tyepof(Callbacl<>).MakegenericType(thegenericType));
new object[] { mydelegate }
Teter28
  • 504
  • 5
  • 8
  • He's asking how to create a delegate based on that type. – Servy Jan 13 '15 at 18:55
  • Delegate.CreateDelegate(typeof(Callback<>).makegenerictype(...), mymethodinfo) – Teter28 Jan 13 '15 at 18:57
  • That's assuming he has a method that is of the appropriate signature. – Servy Jan 13 '15 at 18:57
  • if he want call a delegate without the generic type defined, he can create a dynamic method with the good signature calling his method and create a delegate via dynamicmethod.createdelegate and simply pass it. – Teter28 Jan 13 '15 at 19:00
  • And how should he go about creating a dynamic method of a signature not known at compile time? – Servy Jan 13 '15 at 19:01
  • dynamicmethod id define at runtime. new dynamicmethod(string.empty, tyeof(void), new Type[] { thegenerictargettype }); – Teter28 Jan 13 '15 at 19:04
  • Which of course requires providing the implementation of that method in IL. Not something that a brand new C# developer is likely to be able to do. – Servy Jan 13 '15 at 19:05
  • just provide a minimal implementation.. call another method wich take object as parameter. void mycallback(object arg); in ilgenerator => emit(castclass, typeof(object)); emit(opcodes.call, mycallbackmethodinfo) – Teter28 Jan 13 '15 at 19:07
  • The point remains that your answer has provided only a tiny bit of the solution, much of which is quite non-trivial. Your answer should actually be explaining how to do all of those things for it to actually answer the question. – Servy Jan 13 '15 at 19:08