From 341fb149cbba55b2dcd5cc1e728d614eb7751588 Mon Sep 17 00:00:00 2001 From: Joshua Peterson Date: Tue, 15 Nov 2022 12:53:47 -0500 Subject: [PATCH] Treat instance and static methods as different methods during resolution (#882) When all other comparisons (return type, parameters, etc.) are the same, treat instance and static methods with the same name as different methods. This corrects a problem we noticed in IL2CPP in Unity. --- Mono.Cecil/MetadataResolver.cs | 3 +++ Test/Mono.Cecil.Tests/MethodTests.cs | 15 +++++++++++++++ Test/Resources/il/others.il | 10 ++++++++++ 3 files changed, 28 insertions(+) diff --git a/Mono.Cecil/MetadataResolver.cs b/Mono.Cecil/MetadataResolver.cs index 6511b4187..95d900726 100644 --- a/Mono.Cecil/MetadataResolver.cs +++ b/Mono.Cecil/MetadataResolver.cs @@ -264,6 +264,9 @@ public static MethodDefinition GetMethod (Collection methods, if (!AreSame (method.ReturnType, reference.ReturnType)) continue; + if (method.HasThis != reference.HasThis) + continue; + if (method.IsVarArg () != reference.IsVarArg ()) continue; diff --git a/Test/Mono.Cecil.Tests/MethodTests.cs b/Test/Mono.Cecil.Tests/MethodTests.cs index b878613c0..cef897dd2 100644 --- a/Test/Mono.Cecil.Tests/MethodTests.cs +++ b/Test/Mono.Cecil.Tests/MethodTests.cs @@ -221,5 +221,20 @@ public void ReturnParameterMethod () Assert.IsNotNull (method); Assert.AreEqual (method, method.MethodReturnType.Parameter.Method); } + + [Test] + public void InstanceAndStaticMethodComparison () + { + TestIL ("others.il", module => { + var others = module.GetType ("Others"); + var instance_method = others.Methods.Single (m => m.Name == "SameMethodNameInstanceStatic" && m.HasThis); + var static_method_reference = new MethodReference ("SameMethodNameInstanceStatic", instance_method.ReturnType, others) + { + HasThis = false + }; + + Assert.AreNotEqual(instance_method, static_method_reference.Resolve ()); + }); + } } } diff --git a/Test/Resources/il/others.il b/Test/Resources/il/others.il index 60713af30..22691a2d2 100644 --- a/Test/Resources/il/others.il +++ b/Test/Resources/il/others.il @@ -78,4 +78,14 @@ .other instance void Others::dang_Handler (class [mscorlib]System.EventHandler) .other instance void Others::fang_Handler (class [mscorlib]System.EventHandler) } + + .method public instance void SameMethodNameInstanceStatic() cil managed + { + ret + } + + .method public static void SameMethodNameInstanceStatic() cil managed + { + ret + } // end of static method MethodNameTests::MethodName }