Search code examples
c#genericsreflectionfactory-pattern

Generic Factory method to instantiate a derived class from the base class


I am in the process of creating a factory method which uses a generic abstract type parameter to return the instance of the concrete derived type using reflection. For eg.

public abstract class ServiceClientBase : IServiceClient
{
}

public abstract class Channel : ServiceClientBase
{
}

public class ChannelImpl : Channel
{
}

public class ServiceClientFactory
{
    public T GetService<T>() where T : class, IServiceClient
    {
        // Use reflection to create the derived type instance
        T instance = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string) }, null).Invoke(new object[] { endPointUrl }) as T;
    }
}

Usage:

Channel channelService = factory.GetService<Channel>();

The issue is that I cannot figure out any elegant way for the factory method to instantiate the derived type being passed the abstract base type in the method. The only thing I can come up with is to maintain a Dictionary containing the map between the abstract base and the corresponding derived class but this seems to me like a code smell. Can anybody suggest a better solution.


Solution

  • While you're confident of there being only one implementation, and assuming it's within the same assembly, you could find it via reflection. For example:

    Type implementationType = typeof(T).Assembly.GetTypes()
                                       .Where(t => t.IsSubclassOf(typeof(T))
                                       .Single();
    return (T) Activator.CreateInstance(implementationType);
    

    Of course for performance reasons you may well want to have a cache of abstract type to concrete type.

    If there are multiple implementation classes, you'll need to think of an alternative - one option would be an attribute on the abstract class saying which implementation to use, if that's feasible. (It's hard to give good options without more context.)