2
Vote

Bug in interface member generation

description

I have found that if you have this scenario of inheritance:
 
public class myclass : IBaseClass
{ }
 
public interface IBaseClass : IInheritedClass
{
amethod();
}
 
public interface IInheritedClass
{
bmethod();
}
 
then bmethod() will get generated twice and you will receive this message when trying to establish the proxy: "Method 'bmethod' on type .... is overriding a method that has been overridden." and will fail to connect.
 
You need to change the method GenerateMethodImpl within AbstractClassBuilder to the code below to cope with the issue:

 

 
    protected virtual void GenerateMethodImpl ( TypeBuilder builder, Type currentType, List<Type> alreadyImplemented = null)
    {
        if ( alreadyImplemented == null )
        {
            alreadyImplemented = new List<Type>();
        }
 
        MethodInfo[] methods = currentType.GetMethods();
        foreach (MethodInfo method in methods)
        {
            Type[] parameterTypes = GetParameters(method.GetParameters()); // declare the method with the correct parameters
            MethodBuilder methodBuilder = builder.DefineMethod(method.Name, defaultMethodAttributes, method.ReturnType, parameterTypes);
 
            // Start building the method
            methodBuilder.CreateMethodBody(null, 0);
            ILGenerator iLGenerator = methodBuilder.GetILGenerator();
 
            GenerateMethodImpl(method, parameterTypes, iLGenerator);
 
            // declare that we override the interface method
            builder.DefineMethodOverride(methodBuilder, method);
        }
 
        Type[] inheritedInterfaces = currentType.GetInterfaces();
        foreach (Type inheritedInterface in inheritedInterfaces)
        {
            if(!alreadyImplemented.Contains(inheritedInterface))
            {
                alreadyImplemented.Add(inheritedInterface);
                GenerateMethodImpl( builder, inheritedInterface, alreadyImplemented );
            }
        }
    }

comments