Transform an entity from a type to another I need to transform an entity from an external DLL to another entity which is from a different external DLL. 

My question is: Is this the best solution or is there a better solution (type-save)? I hope there is a better idee because I don't really like to use the dynamic type.

using ExternalDllOne.Fruit; //contains(Apple, Banana, Melon, Peach)
using ExternalDllTwo.Fruit; //contains(ReturnApple, ReturnBanana, ReturnMelon, ReturnPeach)

public static dynamic Transform(Type type, string path)
{
    switch (type.Name)
    {
        case nameof(Banana):
            {
                Banana serializedFruit = SerializerFactory&lt;Banana&gt;.Create(SerializerType.Xml).Deserialize(path);
                ReturnBanana fruit = TransformBanana(serializedFruit);
                return fruit;
            }
        case nameof(Apple):
            {
                Apple serializedFruit = SerializerFactory&lt;Apple&gt;.Create(SerializerType.Xml).Deserialize(path);
                ReturnApple fruit =  TransformApple(serializedFruit);
                return fruit;
            }
        case nameof(Peach):
            {
                Peach serializedFruit = SerializerFactory&lt;Peach&gt;.Create(SerializerType.Xml).Deserialize(path);
                ReturnPeach fruit =  TransformPeach(serializedFruit);
                return fruit;
            }
        case nameof(Melon):
            {
                Melon serializedFruit = SerializerFactory&lt;Melon&gt;.Create(SerializerType.Xml).Deserialize(path);
                ReturnMelon fruit =  TransformMelon(serializedFruit);
                return fruit;
            }
        default:
            throw new ArgumentException($@"Type: [{type.Name}] is not a valid type for this application");
    }
}


The conversion is done thru a facade which is located in an external DLL. They way the Transform&lt;fruit&gt;() works is for every fruit type the same:

private static ReturnBanana TransformBanana(Banana fruit)
{
    var extraSettings = GetBananaSettings();

    var transformedFruit = FruitTransformationFacade.Transform(fruit, extraSettings);

    return transformedFruit;
}


Using the transformer method I have this code:

private static dynamic Load(string path)
{
    XDocument document = FileHelper.ParseFile(path);

    Type fileType = TypeHelper.DetermineFileType(document);

    dynamic component = Transformer.Transform(fileType, path);

    return component;
}


Do I need a wrapper object for each Return&lt;fruit&gt; entity with a coherent Interface, and return the interface?
public static dynamic Transform(Type type, string path)



If I should return an object of any type I'd rather use the object type for it then dynamic. I don't know how you actually use it so I assume that all objects have some set of common properties but don't share a any common interface.

In that case I think it would make more sense to create an artifical wrapper for those objects so that you can read those properties (or methods) without having to use dynamic everywhere.

public class Fruit // = ExternalDllTwoEntityWrapper
{ 
    private readonly dynamic _value;
    Fruit(object obj) =&gt; _value = obj;
    public string Name =&gt; (string)_value.Name;
}


Whether you use dynamic or reflection internally to access those properties is another question.

If you however can change one of those external dlls then an abstraction would be the obvious way to solve this:

public interface IFruit
{
    string Name { get; }
}