Skip to content

Commit

Permalink
Explicit interface implementation name collisions
Browse files Browse the repository at this point in the history
When a proxy is made to implement the same generic interface more than
twice, and that generic interface contains a method that does not use
any generic type parameters, this can be result in a name collision.
This would happen e.g. with a proxy implementing `IObserver<T>` three
times.

DynamicProxy does check for member name collisions, and when such a
one is detected, it switches to explicit interface implementation and
prefixes the member name with the generic interface type name. For the
above example, this will add the following methods to the proxy:

 * OnCompleted
 * IObserver`1.OnCompleted
 * IObserver`1.OnCompleted

It is clearly not sufficient to exclude the concrete type arguments
and use only the number of type parameters. This commit changes the
naming of explicitly interface methods such that they are unique.
  • Loading branch information
stakx committed Jul 4, 2017
1 parent 59d545b commit 77cfec0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/Castle.Core.Tests/BasicInterfaceProxyTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,37 @@ public void Should_implement_explicitly_duplicate_interface_members()
MethodInfo method = type.GetMethod("Foo", BindingFlags.Instance | BindingFlags.Public);
Assert.IsNotNull(method);
Assert.AreSame(method, type.GetTypeInfo().GetRuntimeInterfaceMap(typeof(IIdenticalOne)).TargetMethods[0]);
MethodInfo method2 = type.GetMethod("IIdenticalTwo.Foo", BindingFlags.Instance | BindingFlags.Public);
MethodInfo method2 = type.GetMethod($"{typeof(IIdenticalTwo).FullName}.Foo", BindingFlags.Instance | BindingFlags.Public);
Assert.IsNotNull(method2);
}

[Test]
public void Should_choose_noncolliding_member_names_when_implementing_same_generic_interface_several_times()
{
var proxy = generator.CreateInterfaceProxyWithoutTarget(
typeof(IEmpty),
new[]
{
typeof(IGenericWithNonGenericMethod<bool>),
typeof(IGenericWithNonGenericMethod<int>),
typeof(IGenericWithNonGenericMethod<float>)
});

var type = proxy.GetType().GetTypeInfo();

// The first interface is implemented implicitly:
var boolMethod = type.GetRuntimeInterfaceMap(typeof(IGenericWithNonGenericMethod<bool>)).TargetMethods[0];
Assert.AreEqual("DoSomething", boolMethod.Name);

// The next interface is implemented explicitly to avoid a name collision for `DoSomething`:
var intMethod = type.GetRuntimeInterfaceMap(typeof(IGenericWithNonGenericMethod<int>)).TargetMethods[0];
Assert.AreEqual($"{typeof(IGenericWithNonGenericMethod<int>).FullName}.DoSomething", intMethod.Name);

// The next interface will also be implemented explicitly, for the same reason:
var floatMethod = type.GetRuntimeInterfaceMap(typeof(IGenericWithNonGenericMethod<float>)).TargetMethods[0];
Assert.AreEqual($"{typeof(IGenericWithNonGenericMethod<float>).FullName}.DoSomething", floatMethod.Name);
}

private ParameterInfo[] GetMyTestMethodParams(Type type)
{
MethodInfo methodInfo = type.GetMethod("MyTestMethod", BindingFlags.Instance | BindingFlags.Public);
Expand Down
21 changes: 21 additions & 0 deletions src/Castle.Core.Tests/Interfaces/IGenericWithNonGenericMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests.Interfaces
{
public interface IGenericWithNonGenericMethod<T>
{
void DoSomething();
}
}
2 changes: 1 addition & 1 deletion src/Castle.Core/DynamicProxy/Generators/MetaMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal override void SwitchToExplicitImplementation()
Attributes |= MethodAttributes.SpecialName;
}

name = string.Format("{0}.{1}", Method.DeclaringType.Name, Method.Name);
name = string.Format("{0}.{1}", Method.DeclaringType.FullName, Method.Name);
}

private MethodAttributes ObtainAttributes()
Expand Down

0 comments on commit 77cfec0

Please sign in to comment.