Search code examples
c#.netdynamicreflection.net-assembly

Creating a dynamic fake class inside of an assembly


Probably what I am asking is impossible, but nevertheless here is my problem and question. First of all this is C# and .NET.
I would like to define an empty implementation (empty methods, functions returning defaults) of a class to an interface in such a way if I change the interface I will not need to adapt the code. Unfortunately I can not generate the implementation containing assembly and I can not define a dynamical mock on it because instantiation is done automatically via reflection. Here is the problem explained more broadly:

I have a dll/assembly let us call it IMyInterface.dll that contains an interface: IMyInterface.
Layers above I have the implementation in a separate dll/assembly let us call it MyInterfaceImplementation.dll.
In the middle/between I have an automatized test framework that can depend on IMyInterface.dll but not on MyInterfaceImplementation.dll.
Now this test framework uses the production code infrastructure that instantiates types via reflection. It is a dependency injection type framework. So you say to the production code infrastructure give me this interface implementation from this assembly.
In our case you say give me a IMyInterface from MyInterfaceImplementation.dll. In the test framework you can not depend from MyInterfaceImplementation so you define a fake/stub/mock class based on IMyInterface in a third assembly lets call it: MyInterfaceFakeImplementation.dll
In the test framework you say give me a IMyInterface from MyInterfaceFakeImplementation.dll and you are fine.
Note: Do to our modules hierarchy it is not possible to restructure the dependencies. Regarding Mock Frameworks, I am not controlling the instantiation. Instantiation is done inside the dependency injection framework.

When you write your code in the MyInterfaceFakeImplementation.dll you write down like this:

class MyInterfaceFakeImplementation : IMyInterface 
{
  // IMyInterface implementation. 
}

Now what I would like is to provide is dynamic class of IMyInterface, so when interface changes than I do not need to adapt the fake.

Very shorty here is what I want:

Given:
IMyInterface interface in IMyInterface.dll
MyInterfaceFakeImplementation impementation of IMyInterface in MyInterfaceFakeImplementation.dll
MyInterfaceFakeImplementation has empty functions and returns default values.

When:
I change IMyInterface (Ex. change a functions signature).

Then:
I do not need to change MyInterfaceFakeImplementation, just recompile MyInterfaceFakeImplementation.dll. Note: It is not possible to possible to generate this assembly, needs to be compiled.

Here is a workaround.
Do a fake implementation (class) next to IMyInterface in the IMyInterface.dll, let us call it MyInterfaceFakeBase. In the MyInterfaceFakeImplementation.dll derive the MyInterfaceFakeImplementation from this base class MyInterfaceFakeBase and leave it empty. When changing the interface (IMyInterface) adapt MyInterfaceFakeBase and never worry about MyInterfaceFakeImplementation and MyInterfaceFakeImplementation.dll.

Okie, for those who would like start coding here is a sample Console type App that, might help. Add a class to this code so that it finds a type that implements the interface and if you change the interface you do not need to change that class. (Do not modify Main function.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DynamicFake3
{
    public interface IMyInterface
    {
        void SimpleMethod();
        bool SimpleFunction();
        void OneParameterMethod(int i);
    }

    class Program
    {
        static void Main(string[] args)
        {

            Assembly anAssembly = Assembly.LoadFrom("DynamicFake3.exe");

            foreach (Type aType in anAssembly.GetTypes())
            {
                if (aType.GetInterfaces().Contains(typeof(IMyInterface)))
                {
                    Console.WriteLine(aType.FullName);
                }
            }
        }
    }
}

Bye Laszlo


Solution

  • Well nobody answered it so it is impossible to do something like this. It is possible to create a dynamic decorator or proxy or mock for an interface during runtime or to generate an assembly that has a fake implementation for an interface, but not how I would like it. C#, .NET, CLR does not allows it. Or better said it is a good thing that C# does not allow such dynamical (interpreted) behavior.