public class NewExecutorFactory<T> { public IExecutable<T> Create() { IExecutable<T> executor = new Executor<T>(); Type type = typeof(T); if (typeof(IDisposable).IsAssignableFrom(type)) { executor = CreateInstance(typeof(DisposeExecutor<>), type, executor); } if (typeof(ICloneable).IsAssignableFrom(type)) { executor = CreateInstance(typeof(CloneExecutor<>), type, executor); } if (typeof(IFormattable).IsAssignableFrom(type)) { executor = CreateInstance(typeof(FormatExecutor<>), type, executor); } return executor; } private IExecutable<T> CreateInstance( Type instanceType, Type elementType, IExecutable<T> parent) { Type genericType = instanceType.MakeGenericType(elementType); string? fullName = genericType.FullName; if (string.IsNullOrEmpty(fullName)) { throw new Exception(); } Assembly assembly = genericType.Assembly; IExecutable<T>? instance = (IExecutable<T>?)assembly.CreateInstance(fullName, false, BindingFlags.CreateInstance, null, new object[] { parent }, null, null); if (instance == null) { throw new Exception(); } return instance; } }