Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic method patching unreliable #325

Closed
grant77 opened this issue Jul 25, 2020 · 3 comments
Closed

Generic method patching unreliable #325

grant77 opened this issue Jul 25, 2020 · 3 comments

Comments

@grant77
Copy link

grant77 commented Jul 25, 2020

Hi Pardeike,

I cannot seem to get a method with 2 generic arguments to work. This is my very first day using this thing so maybe I am doing something wrong. In my demo below, I cannot get the "Test3" method to work. The output looks like this:

Prefix Called
Test1 Called
Prefix Called
Test2 Called
Test3 Called
    class Program
    {
        static void Main(string[] args)
        {
            var harmony = new Harmony("abc");
            harmony.PatchAll();

            var dummy = new Dummy();
            dummy.Test1();
            dummy.Test2<int>();
            dummy.Test3<int, string>(); // <== DOESN'T WORK!!!
            Console.ReadLine();
        }
    }

    class Dummy
    {
        public void Test1() => Console.WriteLine("Test1 Called");
        public void Test2<TItem>() => Console.WriteLine("Test2 Called");
        public void Test3<TItem1, TItem2>() => Console.WriteLine("Test3 Called");
    }

    [HarmonyPatch]
    class MyPatches
    {
        static IEnumerable<MethodBase> TargetMethods() => new[]
        {
            typeof(Dummy).GetMethod("Test1"),
            typeof(Dummy).GetMethod("Test2").MakeGenericMethod(typeof(int)),
            typeof(Dummy).GetMethod("Test3").MakeGenericMethod(typeof(int), typeof(string)),
        };
        static void Prefix() => Console.WriteLine("Prefix Called");
    }
@pardeike
Copy link
Owner

What environment is used for these tests? Processor, .NET version. What happens if you add more generics?

@grant77
Copy link
Author

grant77 commented Jul 25, 2020

TargetFramework: netcoreapp3.1
RID: win10-x64
Processor: Intel64 Family 6 Model 63 Stepping 0

I modified the program to add more generics. In doing so, I discovered it works when i use "int" for all my generic type arguments. If I use "string", however, it does not work.

Here is my modified application:

    class Program
    {
        static void Main(string[] args)
        {
            var harmony = new Harmony("abc");
            harmony.PatchAll();

            var dummy = new Dummy();
            dummy.Test1();
            dummy.Test2<int>();
            dummy.Test3<int, int>();
            dummy.Test3<int, string>(); // <== BAD !!!!!!!!!!!!!!!!!!!
            dummy.Test4<int, int, int>();
            dummy.Test5<int, int, int, int>();
            dummy.Test6<int, int, int, int, int>();

            Console.ReadLine();
        }
    }

    class Dummy
    {
        public void Test1() => Console.WriteLine("Test1 Called");
        public void Test2<A>() => Console.WriteLine("Test2 Called");
        public void Test3<A, B>() => Console.WriteLine("Test3 Called");
        public void Test4<A, B, C>() => Console.WriteLine("Test4 Called");
        public void Test5<A, B, C, D>() => Console.WriteLine("Test5 Called");
        public void Test6<A, B, C, D, E>() => Console.WriteLine("Test6 Called");
    }

    [HarmonyPatch]
    class MyPatches
    {
        static IEnumerable<MethodBase> TargetMethods() => new[]
        {
            typeof(Dummy).GetMethod("Test1"),
            typeof(Dummy).GetMethod("Test2").MakeGenericMethod(typeof(int)),
            typeof(Dummy).GetMethod("Test3").MakeGenericMethod(typeof(int), typeof(int)),
            typeof(Dummy).GetMethod("Test3").MakeGenericMethod(typeof(int), typeof(string)), // <== BAD !!!!!!!!!!!!!!!!!!!
            typeof(Dummy).GetMethod("Test4").MakeGenericMethod(typeof(int), typeof(int), typeof(int)),
            typeof(Dummy).GetMethod("Test5").MakeGenericMethod(typeof(int), typeof(int), typeof(int), typeof(int)),
            typeof(Dummy).GetMethod("Test6").MakeGenericMethod(typeof(int), typeof(int), typeof(int), typeof(int), typeof(int)),
        };

        static void Prefix() => Console.WriteLine("Prefix Called");
    }

@pardeike
Copy link
Owner

Since this is a rather tricky issue, I welcome you to participate in the discussion at the official discord: https://discord.gg/y2thPry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants