Skip to content

Commit

Permalink
Treat instance and static methods as different methods during resolut…
Browse files Browse the repository at this point in the history
…ion (#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.
  • Loading branch information
Joshua Peterson authored Nov 15, 2022
1 parent e052ab5 commit 341fb14
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Mono.Cecil/MetadataResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ public static MethodDefinition GetMethod (Collection<MethodDefinition> methods,
if (!AreSame (method.ReturnType, reference.ReturnType))
continue;

if (method.HasThis != reference.HasThis)
continue;

if (method.IsVarArg () != reference.IsVarArg ())
continue;

Expand Down
15 changes: 15 additions & 0 deletions Test/Mono.Cecil.Tests/MethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
});
}
}
}
10 changes: 10 additions & 0 deletions Test/Resources/il/others.il
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 341fb14

Please sign in to comment.