diff --git a/src/ComputeSharp.Shaders/Extensions/DynamicMethod{T}.cs b/src/ComputeSharp.Shaders/Extensions/DynamicMethod{T}.cs index 5185d2ec..1fba1d60 100644 --- a/src/ComputeSharp.Shaders/Extensions/DynamicMethod{T}.cs +++ b/src/ComputeSharp.Shaders/Extensions/DynamicMethod{T}.cs @@ -11,15 +11,20 @@ namespace ComputeSharp.Shaders.Extensions /// A that can be used to easily (lol) create dynamic methods /// /// The type of that will be used to wrap the new methods - public static class DynamicMethod where T : Delegate + internal static class DynamicMethod where T : Delegate { + /// + /// The owner type for new dynamic methods + /// + private static readonly Type OwnerType = typeof(DynamicMethod); + /// /// The return type of the /// private static readonly Type ReturnType; /// - /// The array of types of the arguments of the + /// The types of the arguments of the /// private static readonly Type[] ParameterTypes; @@ -41,15 +46,14 @@ namespace ComputeSharp.Shaders.Extensions /// /// Creates a new for the target owner /// - /// The instance that will own the new method /// An that builds the IL bytecode for the new method /// A new dynamic method wrapped as a [Pure] - public static T New(Type owner, Action builder) + public static T New(Action builder) { // Create a new dynamic method with a unique id string id = $"__IL__{typeof(T).Name}_{Interlocked.Increment(ref _Count)}"; - DynamicMethod method = new DynamicMethod(id, ReturnType, ParameterTypes, owner); + DynamicMethod method = new DynamicMethod(id, ReturnType, ParameterTypes, OwnerType); // Build the IL bytecode ILGenerator il = method.GetILGenerator(); diff --git a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs new file mode 100644 index 00000000..1537d452 --- /dev/null +++ b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs @@ -0,0 +1,28 @@ +using System.Reflection.Emit; +using ComputeSharp.Shaders.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Internals +{ + [TestClass] + [TestCategory("DynamicMethod")] + public class DynamicMethodTests + { + public delegate int Square(int x); + + public void TestSquare() + { + Square square = DynamicMethod.New(il => + { + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Dup); + il.Emit(OpCodes.Mul); + il.Emit(OpCodes.Ret); + }); + + int result = square(2); // result = 2 * 2 + + Assert.IsTrue(result == 4); + } + } +}