4

First I'm using Visual Studio 2005 with .NET Framework 2.0. Unfortunately, I can't use a recent version of VS/.NET.

I need to be able to create a class at runtime that inherits another class and also implements a certain interface. The thing is that the class that needs to be inherited has the sames method signatures of the interface.

For example:

public interface ITestInterface
{
    void Test1();
    string Test2(int a, string b);
}

public class TestClass
{
    public void Test1() { ... }
    public string Test2(int a, string b) { ... }
    public void AnotherMethod() { ... }
}

If I create another class, for example:

public class AnotherClass : TestClass, ITestInterface
{
}

In Visual Studio 2005, it gets compiled with no problems as TestClass already implements all the interface's methods/functions.

If I inspect the generated MSIL code for this class, I can see that it creates a type called AnotherClass that inherits from TestClass and implements ITestInterface as I would expect (no methods/functions implemented as they are already implemented in the base class).

Trying to do this by code at runtime like this:

object GetProxy(Type iface, Type obj)
{
    string name = obj.Name + "Proxy";
    AssemblyBuilder assyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(name), AssemblyBuilderAccess.Run);
    ModuleBuilder modBuilder = assyBuilder.DefineDynamicModule(name);
    TypeBuilder typeBuilder = modBuilder.DefineType(name, TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit, obj, new Type[] { iface });

    Type proxyType = typeBuilder.CreateType(); // Exception here

    return Activator.CreateInstance(proxyType);
}

Throws the following exception:

An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll

Additional information: Method 'Test1' in type 'TestClassProxy' from assembly 'TestClassProxy, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

I can't figure out why the runtime is forcing me to implement the methods of the interface if they are already implemented in the base class.

Do you have some ideas? Maybe an option I'm missing?

  • 1
    Your `AnotherClass` does not compile for me, because the methods in `TestClass` are not `public`. When I fix that, `GetProxy()` also starts working. – svick Oct 29 '15 at 21:06
  • 1
    Possible duplicate of [Creating a DynamicType in .NET implementing an interface but using member implementations from a base class](http://stackoverflow.com/questions/4801537/creating-a-dynamictype-in-net-implementing-an-interface-but-using-member-implem) – Brian Reichle Oct 30 '15 at 08:15
  • Even with TestClass's methods/functions being public, it doesn't work for me. It compiles OK but fails at CreateType() call with the same exception. – andyolivares Oct 30 '15 at 14:45

1 Answers1

1

Based on the information found in this link and this one suggested by Brian, marking the base class methods/functions as virtual solves this particular problem.

I know this imposes a requirement for base classes but that's acceptable in my case.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61