How to override method of base class that haven't override in signature

644 Views Asked by At

Today I discovered an interesting thing. I tried to define a type dynamically using TypeBuilder and tried to 'override' (i.e. replace) a method defined in the base class:

public class Test
{
    public void Method()
    {
        Console.WriteLine("Test from Test");
    }
}

public void Bind(string methodToReplace, Action expr)
{
    @object = Activator.CreateInstance<T>();
    Type objectType = @object.GetType();
    AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Mock"), AssemblyBuilderAccess.Run);
    ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("Mock");
    TypeBuilder tB = modBuilder.DefineType(objectType.Name, TypeAttributes.Public | TypeAttributes.Class, objectType);
    var mB = tB.DefineMethod(methodToReplace, MethodAttributes.Public);
    ILGenerator ilGen = mB.GetILGenerator();
    ilGen.EmitCall(OpCodes.Call, expr.GetType().GetMethod("Invoke"), null);
    ilGen.Emit(OpCodes.Ret);
    @object = (T)Activator.CreateInstance(tB.CreateType());
}

But, unfortunetly, in the defined type there are two methods with the same name 'Method', ('t' is an instance of the dynamically defined type):

t.GetType().GetMethods()
{System.Reflection.MethodInfo[6]}
    [0]: {Void Method()}
    [1]: {Void Method()}
    [2]: {System.String ToString()}
    [3]: {Boolean Equals(System.Object)}
    [4]: {Int32 GetHashCode()}
    [5]: {System.Type GetType()}

So my question is: how to add a new method that hides the base class's implementation, equivalent to using the C# 'new' keyword when defining a new method, e.g.:

public class Test2 : Test
{
    new public void Method()
    {
    }
}
1

There are 1 best solutions below

0
On

You can see two methods because you used type.GetMethods() without bindingflags. Try to use bindingflags.declaredonly.

You can see only one method. It is exactly what you want to to.

new keyword is a c# thing to avoid hiding base method by mistake.

it force you to use override or new to avoid warning.

if you want to override base merhod, use method DefineMethodOverride on TypeBuilder.