refactor: Removing unused code

This commit is contained in:
Chris Cameron
2018-11-08 11:46:14 -05:00
parent 8b1c53ebe1
commit 376f9c0837

View File

@@ -24,7 +24,6 @@ namespace ICD.Common.Utils
private static readonly IcdHashSet<Assembly> s_CachedAssemblies; private static readonly IcdHashSet<Assembly> s_CachedAssemblies;
private static readonly IcdHashSet<Type> s_CachedTypes; private static readonly IcdHashSet<Type> s_CachedTypes;
private static readonly Dictionary<Attribute, MethodInfo> s_AttributeToMethodCache;
private static readonly Dictionary<Attribute, Type> s_AttributeToTypeCache; private static readonly Dictionary<Attribute, Type> s_AttributeToTypeCache;
private static readonly Dictionary<Type, IcdHashSet<Attribute>> s_TypeToAttributesCache; private static readonly Dictionary<Type, IcdHashSet<Attribute>> s_TypeToAttributesCache;
@@ -38,26 +37,12 @@ namespace ICD.Common.Utils
s_CachedAssemblies = new IcdHashSet<Assembly>(); s_CachedAssemblies = new IcdHashSet<Assembly>();
s_CachedTypes = new IcdHashSet<Type>(); s_CachedTypes = new IcdHashSet<Type>();
s_AttributeToMethodCache = new Dictionary<Attribute, MethodInfo>();
s_AttributeToTypeCache = new Dictionary<Attribute, Type>(); s_AttributeToTypeCache = new Dictionary<Attribute, Type>();
s_TypeToAttributesCache = new Dictionary<Type, IcdHashSet<Attribute>>(); s_TypeToAttributesCache = new Dictionary<Type, IcdHashSet<Attribute>>();
} }
#region Caching #region Caching
/// <summary>
/// Pre-emptively caches the given assemblies for lookup.
/// </summary>
/// <param name="assemblies"></param>
public static void CacheAssemblies(IEnumerable<Assembly> assemblies)
{
if (assemblies == null)
throw new ArgumentNullException("assemblies");
foreach (Assembly assembly in assemblies)
CacheAssembly(assembly);
}
/// <summary> /// <summary>
/// Pre-emptively caches the given assembly for lookup. /// Pre-emptively caches the given assembly for lookup.
/// </summary> /// </summary>
@@ -70,6 +55,8 @@ namespace ICD.Common.Utils
if (s_CachedAssemblies.Contains(assembly)) if (s_CachedAssemblies.Contains(assembly))
return true; return true;
s_CachedAssemblies.Add(assembly);
#if SIMPLSHARP #if SIMPLSHARP
CType[] types; CType[] types;
#else #else
@@ -114,7 +101,6 @@ namespace ICD.Common.Utils
foreach (var type in types) foreach (var type in types)
CacheType(type); CacheType(type);
s_CachedAssemblies.Add(assembly);
return true; return true;
} }
@@ -133,44 +119,23 @@ namespace ICD.Common.Utils
if (s_CachedTypes.Contains(type)) if (s_CachedTypes.Contains(type))
return; return;
s_CachedTypes.Add(type); s_CachedTypes.Add(type);
MethodInfo[] methods;
try try
{ {
s_TypeToAttributesCache[type] = new IcdHashSet<Attribute>(type.GetCustomAttributes<Attribute>(false)); s_TypeToAttributesCache[type] = new IcdHashSet<Attribute>(type.GetCustomAttributes<Attribute>(false));
foreach (Attribute attribute in s_TypeToAttributesCache[type]) foreach (Attribute attribute in s_TypeToAttributesCache[type])
s_AttributeToTypeCache[attribute] = type; s_AttributeToTypeCache[attribute] = type;
methods = type.GetMethods();
} }
// GetMethods for Open Generic Types is not supported. // GetMethods for Open Generic Types is not supported.
catch (NotSupportedException) catch (NotSupportedException)
{ {
return;
} }
// Not sure why this happens :/ // Not sure why this happens :/
catch (InvalidProgramException) catch (InvalidProgramException)
{ {
return;
} }
foreach (MethodInfo method in methods)
CacheMethod(method);
}
/// <summary>
/// Caches the method.
/// </summary>
/// <param name="method"></param>
private static void CacheMethod(MethodInfo method)
{
if (method == null)
throw new ArgumentNullException("method");
foreach (Attribute attribute in ReflectionExtensions.GetCustomAttributes<Attribute>(method, false))
s_AttributeToMethodCache[attribute] = method;
} }
#endregion #endregion
@@ -252,32 +217,6 @@ namespace ICD.Common.Utils
return s_AttributeToTypeCache[attribute]; return s_AttributeToTypeCache[attribute];
} }
/// <summary>
/// Gets all of the cached method attributes of the given type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IEnumerable<T> GetMethodAttributes<T>()
where T : Attribute
{
return s_AttributeToMethodCache.Select(p => p.Key)
.OfType<T>()
.ToArray();
}
/// <summary>
/// Gets the cached method for the given attribute.
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static MethodInfo GetMethod(Attribute attribute)
{
if (attribute == null)
throw new ArgumentNullException("attribute");
return s_AttributeToMethodCache[attribute];
}
#endregion #endregion
} }
} }