private static void UpdateDependencyBases() { DependencyBases.Clear(); DependencyBaseLatestImplementations.Clear(); foreach (var @base in DI.GetAllTypes().Where(t => t.IsAbstract || t.IsInterface)) { if (@base.IsInterface || @base.BaseType == typeof(object)) { // Interface implements an interface that implements IDependencyBase foreach (var type1 in @base.GetInterfaces(false).Where(i => i.FullName() != typeof(IDependencyBase).FullName())) { if (type1.GetInterfaces().Any(i => i.FullName() == typeof(IDependencyBase).FullName())) { if (!DependencyBases.ContainsKey(type1.FullName())) { DependencyBases.Add(type1.FullName(), new HashSet <string>()); } DependencyBases[type1.FullName()].Add(@base.FullName()); break; } } } else { if (@base.BaseType.GetInterfaces(false).Any(i => i.FullName() == typeof(IDependencyBase).FullName())) { if (!DependencyBases.ContainsKey(@base.BaseType.FullName())) { DependencyBases.Add(@base.BaseType.FullName(), new HashSet <string>()); } DependencyBases[@base.BaseType.FullName()].Add(@base.FullName()); } } } var types = DI.GetAllTypes().Where(t => t.IsClass && !t.IsAbstract).ToList(); foreach (var pair in DependencyBases) { foreach (var type in pair.Value) { var tuple = VersionUtils.GetLastImplementation(Version, types.Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, type, false))); if (tuple != null && tuple.Value.Type != null) { if (!DependencyBaseLatestImplementations.ContainsKey(type)) { DependencyBaseLatestImplementations.Add(type, tuple.Value.Type); } else { DependencyBaseLatestImplementations[type] = tuple.Value.Type; } } } } }
internal static object?GetImplementation(Type baseType, params object[] args) { var types = DI.GetAllTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, baseType)); var tuple = VersionUtils.GetLastImplementation(Version, types); return(tuple != null?Activator.CreateInstance(tuple?.Type, args) : null); }
private static void UpdateDependencies() { var interfaces = DI.GetAllTypes() .Where(t => t.IsAbstract || t.IsInterface) .Where(t => t.GetInterfaces(false).Any(i => i.FullName == typeof(IDependency).FullName)); foreach (var @interface in interfaces) { var types = DI.GetAllTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, @interface)); var tuple = VersionUtils.GetLastImplementation(Version, types); if (tuple != null && tuple.Value.Type != null) { LatestImplementations.Add(@interface, tuple.Value.Type); } } }
/// <summary> /// Foolproofing. Custom implementations can be wrapped, but we can't be sure if they are correct. /// Do checking until we find the first correct one /// </summary> private static object?GetImplementationRecursive(Type baseType, Type wrapperType, object[] args, IEnumerable <Type> types) { var tuple = VersionUtils.GetLastImplementation(Version, types); if (tuple == null || tuple?.Type == null) { return(null); } var obj = Activator.CreateInstance(tuple?.Type, args); if (baseType.IsAssignableFrom(tuple?.Type)) { return(obj); } else { var wrapperObject = Activator.CreateInstance(wrapperType, obj); return(wrapperObject is IWrapper wrapper && wrapper.IsCorrect ? wrapperObject : GetImplementationRecursive(baseType, wrapperType, args, types.Where(t => t != tuple?.Type))); } }
private static void UpdateDependencies() { LatestImplementations.Clear(); var bases = DI.GetAllTypes() .Where(t => t.IsAbstract || t.IsInterface) .Where(t => t.GetInterfaces(false).Any(i => i.FullName() == typeof(IDependency).FullName()) && t.GetInterfaces(true).All(i => FullName(i) != typeof(IWrapper).FullName())) .Select(i => i.FullName()) .Distinct(); foreach (var @base in bases) { var types = DI.GetAllTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, @base)); var tuple = VersionUtils.GetLastImplementation(Version, types); if (tuple != null && tuple.Value.Type != null) { LatestImplementations.Add(@base, tuple.Value.Type); } } }