Skip to content

Instantly share code, notes, and snippets.

@Vake93
Created August 30, 2021 14:42
Show Gist options
  • Save Vake93/9f9e8f793cab37ec23f10ee9d4968281 to your computer and use it in GitHub Desktop.
Save Vake93/9f9e8f793cab37ec23f10ee9d4968281 to your computer and use it in GitHub Desktop.
Dynamic Method Experiments
using System.Reflection.Emit;
var dynamicMethod = new DynamicMethod("MethodA", typeof(void), Array.Empty<Type>());
var methodIL = dynamicMethod.GetILGenerator();
methodIL.Emit(OpCodes.Newobj, typeof(Random).GetConstructor(Array.Empty<Type>())!);
methodIL.EmitCall(OpCodes.Call, typeof(Test).GetMethod(nameof(Test.MethodB))!, null);
methodIL.Emit(OpCodes.Ret);
dynamicMethod.Invoke(null, null);
public class Test
{
public static void MethodB(string text)
{
var random = (Random)((object)text);
Console.WriteLine($"Random Number: {random.Next()}");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment