public interface IMyInterface<in T>
public class FirstImpl<T> : IMyInterface<T> where T : B
public class SecondImpl<T> : IMyInterface<T> where T : C
public static void Main()
Console.WriteLine("test1 " + test1.GetType().Name);
Console.WriteLine("test2 " + test2.GetType().Name);
public static IMyInterface<T> Create<T>()where T : A
if (typeof (B).IsAssignableFrom(typeof (T)))
return CreateImplementation<T>(typeof (FirstImpl<>));
else if (typeof (C).IsAssignableFrom(typeof (T)))
return CreateImplementation<T>(typeof (SecondImpl<>));
throw new ArgumentException("unknown type " + typeof (T).Name);
private static IMyInterface<T> CreateImplementation<T>(Type implementationType)where T : A
var boundType = implementationType.MakeGenericType(typeof (T));
return Activator.CreateInstance(boundType) as IMyInterface<T>;