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

Add missing equality checks to MethodSignatureComparer.EqualSignatureTypes #310

Merged
merged 4 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Castle Core Changelog

## Unreleased

Bugfixes:
- Add missing equality checks in `MethodSignatureComparer.EqualSignatureTypes` to fix `TypeLoadException`s ("Method does not have an implementation") (@stakx, #310)

## 4.2.0 (2017-09-28)

Enhancements:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ public void NonVirtualExplicitInterfaceMethods_AreIgnored_OnClassProxy()

Assert.AreEqual(5, result);
}

[Test]
public void CreateClassProxy_GivenAdditionalInterfaceWithOverloadedGenericMethodsHavingGenericParameter_SuccessfullyCreatesProxyInstance()
{
var instance = generator.CreateClassProxy(typeof(object), new Type[] { typeof(InterfaceWithOverloadedGenericMethod) }, interceptor);
Assert.NotNull(instance);
}
}

public class ExplicitInterfaceWithPropertyImplementation : ISimpleInterfaceWithProperty
Expand All @@ -185,4 +192,18 @@ public int Age
get { throw new NotImplementedException(); }
}
}

public interface InterfaceWithOverloadedGenericMethod
{
void GenericMethod<T>(GenericClass1<T> arg);
void GenericMethod<T>(GenericClass2<T> arg);
}

public class GenericClass1<T>
{
}

public class GenericClass2<T>
{
}
}
23 changes: 23 additions & 0 deletions src/Castle.Core.Tests/MethodComparerTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,28 @@ public void Compare_inherited_double_nested_return_method()
Assert.IsTrue(mc.Equals(typeof(Base).GetMethod("GenericMethod7"),
typeof(Inherited).GetMethod("GenericMethod7")));
}

[Test]
public void Compare_two_method_overloads_with_generic_arg_types()
{
var mc = MethodSignatureComparer.Instance;
var methods = typeof(IHaveOverloadedGenericMethod).GetMethods();
var firstOverload = methods[0];
var secondOverload = methods[1];

// The overloads must obviously have different signatures, or we could never even have
// compiled this test code successfully. The arguments must have a different type:
Assert.IsFalse(mc.Equals(firstOverload, secondOverload));
}

private interface IHaveOverloadedGenericMethod
{
void GenericMethod<T>(GenericClass1<T> arg);
void GenericMethod<T>(GenericClass2<T> arg);
}

private class GenericClass1<T> { }

private class GenericClass2<T> { }
Copy link
Member Author

@stakx stakx Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt it was important to keep the two tests separate:

  • The unit test here deals with some internal cogwheel;
  • The regression unit test acts at the level of the public API.

Yet, declaring the same set of test types (the interface, plus GenericClass1<T> and GenericClass2<T>) twice seems somewhat repetitive. Do you mind? (If so, where would be the best place to put them?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yet, declaring the same set of test types (the interface, plus GenericClass1 and GenericClass2) twice seems somewhat repetitive. Do you mind? (If so, where would be the best place to put them?)

Two tests sounds fine to me, means if the internal implementation changes we'll at least still have the regression one.

}
}
23 changes: 19 additions & 4 deletions src/Castle.Core/DynamicProxy/Generators/MethodSignatureComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,35 @@ public bool EqualParameters(MethodInfo x, MethodInfo y)

public bool EqualSignatureTypes(Type x, Type y)
{
if (x.GetTypeInfo().IsGenericParameter != y.GetTypeInfo().IsGenericParameter)
var xti = x.GetTypeInfo();
var yti = y.GetTypeInfo();

if (xti.IsGenericParameter != yti.IsGenericParameter)
{
return false;
}
else if (xti.IsGenericType != yti.IsGenericType)
{
return false;
}

if (x.GetTypeInfo().IsGenericParameter)
if (xti.IsGenericParameter)
{
if (x.GetTypeInfo().GenericParameterPosition != y.GetTypeInfo().GenericParameterPosition)
if (xti.GenericParameterPosition != yti.GenericParameterPosition)
{
return false;
}
}
else if (x.GetTypeInfo().IsGenericType)
else if (xti.IsGenericType)
{
var xGenericTypeDef = xti.GetGenericTypeDefinition();
var yGenericTypeDef = yti.GetGenericTypeDefinition();

if (xGenericTypeDef != yGenericTypeDef)
{
return false;
}

var xArgs = x.GetGenericArguments();
var yArgs = y.GetGenericArguments();

Expand Down