Created
August 30, 2021 14:42
-
-
Save Vake93/9f9e8f793cab37ec23f10ee9d4968281 to your computer and use it in GitHub Desktop.
Dynamic Method Experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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