diff --git a/Harmony/Internal/PatchTools.cs b/Harmony/Internal/PatchTools.cs index 34a4f01c..b7ee673e 100644 --- a/Harmony/Internal/PatchTools.cs +++ b/Harmony/Internal/PatchTools.cs @@ -41,14 +41,43 @@ static Assembly GetExecutingAssemblyReplacement() public static MethodInfo CreateMethod(string name, Type returnType, List> parameters, Action generator) { var parameterTypes = parameters.Select(p => p.Value).ToArray(); + + if (AccessTools.IsMonoRuntime && HarmonyLib.Tools.isWindows == false) + { + var assemblyName = new AssemblyName("TempAssembly"); + +#if NET2 || NET35 + var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); +#else + var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); +#endif + + var moduleBuilder = assemblyBuilder.DefineDynamicModule("TempModule"); + var typeBuilder = moduleBuilder.DefineType("TempType", TypeAttributes.Public); + + var methodBuilder = typeBuilder.DefineMethod(name, + MethodAttributes.Public | MethodAttributes.Static, + returnType, parameterTypes); + + for (var i = 0; i < parameters.Count; i++) + methodBuilder.DefineParameter(i + 1, ParameterAttributes.None, parameters[i].Key); + + generator(methodBuilder.GetILGenerator()); + +#if NETSTANDARD2_0 + var createdType = typeBuilder.CreateTypeInfo().AsType(); +#else + var createdType = typeBuilder.CreateType(); +#endif + return createdType.GetMethod(name, BindingFlags.Public | BindingFlags.Static); + } + var dynamicMethod = new DynamicMethodDefinition(name, returnType, parameterTypes); for (var i = 0; i < parameters.Count; i++) dynamicMethod.Definition.Parameters[i].Name = parameters[i].Key; - var il = dynamicMethod.GetILGenerator(); - generator(il); - + generator(dynamicMethod.GetILGenerator()); return dynamicMethod.Generate(); } diff --git a/HarmonyTests/Patching/DynamicMethodPatches.cs b/HarmonyTests/Patching/DynamicMethodPatches.cs index b1b7f054..96e1e280 100644 --- a/HarmonyTests/Patching/DynamicMethodPatches.cs +++ b/HarmonyTests/Patching/DynamicMethodPatches.cs @@ -9,13 +9,6 @@ public class DynamicMethodPatches : TestLogger [Test] public void Test_ByRefResultPrefix() { - if (AccessTools.IsMonoRuntime && HarmonyLib.Tools.isWindows == false) - { - // no longer works with linux/mac and mono - // System.NotSupportedException : Custom attributes on a ParamInfo with member System.Reflection.Emit.DynamicMethod are not supported - return; - } - var originalClass = typeof(Assets.Class11); Assert.NotNull(originalClass);