Search code examples
.netinterfaceautomappersealed

Is it possible to use AutoMapper to wrap methods?


I have two classes:

public class TestClass1
{
    public int TestInt { get; set; }

    public void TestMethod()
    {
        // Do something
    }
}

public class TestClass2
{
    public int TestInt { get; set; }

    public void TestMethod()
    {
        // Do something
    }
}

I want to create interface that I can use for both classes. The easiest solution is to implement the interface on TestClass1 and TestClass2 but I don;t have access to the implementation of these classes (external dll). I was wondering if I can create new interface and use AutoMapper to map TestClass1 and TestClass2 to ITestInterface:

public interface ITestInterface
{
    int TestInt { get; set; }

    void TestMethod();
}

Solution

  • You say you require mapping "TestClass1 and TestClass2 to ITestInterface", however you'd need an instance of a class to map to as you can't create an instance of an interface.

    I assume you're trying to treat the classes interchangeably by converting them to the same interface. If so Automapper isn't what you should be looking at - see this question on stack-overflow for some details on how to manage treating the classes as if they both implement the same interface (even if you don't have access to the source code).