Skip to content

Commit 341fb14

Browse files
author
Joshua Peterson
authored
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.
1 parent e052ab5 commit 341fb14

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Mono.Cecil/MetadataResolver.cs

+3
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ public static MethodDefinition GetMethod (Collection<MethodDefinition> methods,
264264
if (!AreSame (method.ReturnType, reference.ReturnType))
265265
continue;
266266

267+
if (method.HasThis != reference.HasThis)
268+
continue;
269+
267270
if (method.IsVarArg () != reference.IsVarArg ())
268271
continue;
269272

Test/Mono.Cecil.Tests/MethodTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,20 @@ public void ReturnParameterMethod ()
221221
Assert.IsNotNull (method);
222222
Assert.AreEqual (method, method.MethodReturnType.Parameter.Method);
223223
}
224+
225+
[Test]
226+
public void InstanceAndStaticMethodComparison ()
227+
{
228+
TestIL ("others.il", module => {
229+
var others = module.GetType ("Others");
230+
var instance_method = others.Methods.Single (m => m.Name == "SameMethodNameInstanceStatic" && m.HasThis);
231+
var static_method_reference = new MethodReference ("SameMethodNameInstanceStatic", instance_method.ReturnType, others)
232+
{
233+
HasThis = false
234+
};
235+
236+
Assert.AreNotEqual(instance_method, static_method_reference.Resolve ());
237+
});
238+
}
224239
}
225240
}

Test/Resources/il/others.il

+10
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,14 @@
7878
.other instance void Others::dang_Handler (class [mscorlib]System.EventHandler)
7979
.other instance void Others::fang_Handler (class [mscorlib]System.EventHandler)
8080
}
81+
82+
.method public instance void SameMethodNameInstanceStatic() cil managed
83+
{
84+
ret
85+
}
86+
87+
.method public static void SameMethodNameInstanceStatic() cil managed
88+
{
89+
ret
90+
} // end of static method MethodNameTests::MethodName
8191
}

0 commit comments

Comments
 (0)