Dans cet exemple de code, j'essaie d'invoquer une action anonyme de la part d'un générateur. Je ne suis pas sûr si et comment je peux charger la référence au délégué et comment l'appeler. Je peux le faire si le OnFunctionCall
est une méthode statique et non une propriété.Comment appeler Action <chaîne, booléenne> à partir d'un générateur
public delegate void TestDelegate();
public static class ExampleOne
{
public static Action<string, bool> OnFunctionCall
=> (message, flag) => Console.WriteLine("Example");
}
public static class ExampleTwo
{
public static TType CreateDelegate<TType>(Action<string, bool> onFunctionCall)
where TType : class
{
var method = new DynamicMethod($"{Guid.NewGuid()}", typeof(void), Type.EmptyTypes, typeof(TType), true);
ILGenerator il = method.GetILGenerator();
// Emit some code that invoke unmanaged function ...
// loading the first string argument
il.Emit(OpCodes.Ldstr, method.Name);
// not sure here how to load boolean value to the stack
il.Emit(OpCodes.Ldc_I4_0);
// this line doesn't work
// example two has no idea about ExampleOne
// is it possible to load the reference of the Action<string, bool> to the stack and call it ?
il.Emit(OpCodes.Call, onFunctionCall.Method);
il.Emit(OpCodes.Ret);
return method.CreateDelegate(typeof(TestDelegate)) as TType;
}
}
public class Program
{
public static void Main(string[] args)
=> ExampleTwo
.CreateDelegate<TestDelegate>(ExampleOne.OnFunctionCall)
.Invoke();
}
Il est un diable de beaucoup plus facile à utiliser que CodeDOM Émettre – MickyD
@MickyD Reflection Pouvez-vous donner un exemple – ptp
@MickyD Il doit être compatible avec la norme 2.0 trop nette – ptp