Harmony: Harmony should support MS .NET and not only Mono. (was: Error trying to override int getter)

Target class:

public sealed class VisualFind
{
    private int _waitForElementsTimeout;

    public int WaitOnElementsTimeout
    {
      get
      {
        return this._waitForElementsTimeout;
      }
      set
      {
        this._waitForElementsTimeout = value;
    }
}

Patcher:

internal class HarmonyPatcher
{
        private static readonly Logger _Log = LogManager.GetCurrentClassLogger();

        public static HarmonyInstance Initialize()
        {
            _Log.Info($"Initializing harmony patches");
            HarmonyInstance harmony = HarmonyInstance.Create("com.test");
            harmony.PatchAll(Assembly.GetExecutingAssembly());
            return harmony;
        }
}

Patch:

[HarmonyPatch(typeof(VisualFind))]
[HarmonyPatch("get_WaitOnElementsTimeout")]
public class VisualFindPatch
{
        private static readonly Logger _Log = LogManager.GetCurrentClassLogger();

        [HarmonyPrefix]
        static void BeforeGet()
        {

        }
}

I get the following error:

2017-02-06 14:18:45.0133 INFO Test.Ui.Patches.HarmonyPatcher.Initialize(Main.cs:19)   Initializing harmony patches
2017-02-06 14:18:45.0653 FATAL Test.Ui.Utility.Test.BaseTest.Initialize(BaseTest.cs:114) System.NotSupportedException: Wrong MethodAttributes or CallingConventions for DynamicMethod. Only public, static, standard supported
   at System.Reflection.Emit.DynamicMethod.CheckConsistency(MethodAttributes attributes, CallingConventions callingConvention)
   at System.Reflection.Emit.DynamicMethod.Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, Boolean skipVisibility, Boolean transparentMethod, StackCrawlMark& stackMark)
   at System.Reflection.Emit.DynamicMethod..ctor(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility)
   at Harmony.DynamicTools.CreateDynamicMethod(MethodBase original, String suffix) in C:\repo\Harmony\Harmony\Tools\DynamicTools.cs:line 22
   at Harmony.MethodPatcher.CreatePatchedMethod(MethodBase original, List`1 prefixes, List`1 postfixes, List`1 processors) in C:\repo\Harmony\Harmony\MethodPatcher.cs:line 51
   at Harmony.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo) in C:\repo\Harmony\Harmony\PatchFunctions.cs:line 70
   at Harmony.PatchProcessor.Patch() in C:\repo\Harmony\Harmony\PatchProcessor.cs:line 57
   at Harmony.HarmonyInstance.<PatchAll>b__5_0(Type type) in C:\repo\Harmony\Harmony\HarmonyInstance.cs:line 60
   at Harmony.CollectionExtensions.Do[T](IEnumerable`1 sequence, Action`1 action) in C:\repo\Harmony\Harmony\Tools\Extensions.cs:line 45
   at Harmony.HarmonyInstance.PatchAll(Assembly assembly) in C:\repo\Harmony\Harmony\HarmonyInstance.cs:line 53
   at Test.Ui.Patches.HarmonyPatcher.Initialize() in C:\repo\Test.Ui\Patches\Main.cs:line 21

To my knowledge, it should be working properly. What am I missing?

I have also tried changing the signature to static bool BeforeGetWait and static bool AfterGetWait, with same behavior and error message.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I prioritized Mono but that does not mean that with a few checks one couldn’t add .NET compatibility. I think I address this in the future or let someone else do it in a pull request. I am pretty sure that it is possible to have one version of Harmony that supports both environments.