public void Initialize() { Type type = typeof(IConfig); IEnumerable <Type> interfaces = _typeDiscovery.FindNethermindTypes(type).Where(x => x.IsInterface); foreach (Type @interface in interfaces) { Type implementation = _typeDiscovery.FindNethermindTypes(@interface).SingleOrDefault(); if (implementation != null) { Categories.Add(@interface.Name.Substring(1), Activator.CreateInstance(implementation)); _implementations[@interface] = implementation; } } }
public static Type GetDirectInterfaceImplementation(this Type interfaceType) { if (!interfaceType.IsInterface) { throw new NotSupportedException($"GetDirectInterfaceImplementation method is only allowed to use on interface types, got {interfaceType} instead"); } TypeDiscovery typeDiscovery = new TypeDiscovery(); Type[] baseInterfaces = interfaceType.GetInterfaces(); IEnumerable <Type> implementations = typeDiscovery.FindNethermindTypes(interfaceType).Where(i => i.IsClass); foreach (Type implementation in implementations) { List <Type> interfaces = implementation.GetInterfaces().ToList(); interfaces.RemoveAll(i => baseInterfaces.Contains(i)); if (interfaces.Contains(interfaceType) && interfaces.Count() == 1) { return(implementation); } } throw new InvalidOperationException($"Couldn't find direct implementation of {interfaceType} interface"); }