mardi 9 juin 2015

C# check if there is an overload method with the specific type

the title is maybe not that clear, I'll try to explain on an example.

I'm replacing the AutoMapper with a custom DTO mapper, and at the time need both of them to work. It there is a custom implementation use it, else fallback to AutoMapper.

The custom mapper looks something like this:

public static class CustomMapper
{
    public static Entity1DTO Map(Entity1 source){
        ... some mapping code ...
    }

    public static Entity2DTO Map(Entity2 source){
        ... some mapping code ...
    }

    ...
}

I need a way to check if the Map method excepts a specific type without checking for each type manually. I tried creating another overload which accepts object and returns object, but I don't know how to check if other overloads satisfy a specific type and call them.

Basically I want to make a generic wrapper method which will handle redirecting to CustomMapper or AutoMapper. Something like:

public static class Mapper
{
    public static T Map<T>(object source){
        return CustomMapper.Map(source) ?? AutoMapper.Map(source);
    }
}

So the question is how do I make a method in CustomMapper that will know if another Map method overload can handle the accepted type. If not return null or throw an exception, so I can fallback to AutoMapper in the wrapper.





Aucun commentaire:

Enregistrer un commentaire