mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Refactor GetInstances method
This commit is contained in:
parent
d4bfdbd7b8
commit
eefbd00925
2 changed files with 8 additions and 9 deletions
|
@ -1351,18 +1351,17 @@ public static T ByteArrayToStructure<T>(byte[] bytes) where T : struct
|
|||
}
|
||||
}
|
||||
|
||||
public static T[] GetInstances<T>() where T : class
|
||||
public static IEnumerable<T> GetInstances<T>() where T : class
|
||||
{
|
||||
IEnumerable<T> instances = from t in Assembly.GetCallingAssembly().GetTypes()
|
||||
where t.IsClass && t.IsSubclassOf(typeof(T)) && t.GetConstructor(Type.EmptyTypes) != null
|
||||
select Activator.CreateInstance(t) as T;
|
||||
|
||||
return instances.ToArray();
|
||||
Type baseType = typeof(T);
|
||||
Assembly assembly = baseType.Assembly;
|
||||
return assembly.GetTypes().Where(t => t.IsClass && t.IsSubclassOf(baseType) && t.GetConstructor(Type.EmptyTypes) != null).
|
||||
Select(t => Activator.CreateInstance(t) as T);
|
||||
}
|
||||
|
||||
public static IEnumerable<Type> FindSubclassesOf<TBaseType>()
|
||||
public static IEnumerable<Type> FindSubclassesOf<T>()
|
||||
{
|
||||
Type baseType = typeof(TBaseType);
|
||||
Type baseType = typeof(T);
|
||||
Assembly assembly = baseType.Assembly;
|
||||
return assembly.GetTypes().Where(t => t.IsSubclassOf(baseType));
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ public static class UploaderFactory
|
|||
|
||||
private static Dictionary<T, T2> CacheServices<T, T2>() where T2 : UploaderService<T>
|
||||
{
|
||||
T2[] instances = Helpers.GetInstances<T2>();
|
||||
IEnumerable<T2> instances = Helpers.GetInstances<T2>();
|
||||
|
||||
AllServices.AddRange(instances.OfType<IUploaderService>());
|
||||
AllGenericUploaderServices.AddRange(instances.OfType<IGenericUploaderService>());
|
||||
|
|
Loading…
Reference in a new issue