mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
refactor: Removing unused code, loosening attribute type constraints
This commit is contained in:
@@ -2,10 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
using ICD.Common.Utils.Collections;
|
|
||||||
using ICD.Common.Utils.Extensions;
|
using ICD.Common.Utils.Extensions;
|
||||||
using ICD.Common.Utils.Services;
|
|
||||||
using ICD.Common.Utils.Services.Logging;
|
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
using Crestron.SimplSharp.Reflection;
|
using Crestron.SimplSharp.Reflection;
|
||||||
#else
|
#else
|
||||||
@@ -20,139 +17,6 @@ namespace ICD.Common.Utils
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class AttributeUtils
|
public static class AttributeUtils
|
||||||
{
|
{
|
||||||
// Avoid caching the same assembly multiple times.
|
|
||||||
private static readonly IcdHashSet<Assembly> s_CachedAssemblies;
|
|
||||||
private static readonly IcdHashSet<Type> s_CachedTypes;
|
|
||||||
|
|
||||||
private static readonly Dictionary<Attribute, Type> s_AttributeToTypeCache;
|
|
||||||
private static readonly Dictionary<Type, IcdHashSet<Attribute>> s_TypeToAttributesCache;
|
|
||||||
|
|
||||||
private static ILoggerService Logger { get { return ServiceProvider.TryGetService<ILoggerService>(); } }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Constructor.
|
|
||||||
/// </summary>
|
|
||||||
static AttributeUtils()
|
|
||||||
{
|
|
||||||
s_CachedAssemblies = new IcdHashSet<Assembly>();
|
|
||||||
s_CachedTypes = new IcdHashSet<Type>();
|
|
||||||
|
|
||||||
s_AttributeToTypeCache = new Dictionary<Attribute, Type>();
|
|
||||||
s_TypeToAttributesCache = new Dictionary<Type, IcdHashSet<Attribute>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Caching
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Pre-emptively caches the given assembly for lookup.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assembly"></param>
|
|
||||||
public static bool CacheAssembly(Assembly assembly)
|
|
||||||
{
|
|
||||||
if (assembly == null)
|
|
||||||
throw new ArgumentNullException("assembly");
|
|
||||||
|
|
||||||
if (s_CachedAssemblies.Contains(assembly))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
s_CachedAssemblies.Add(assembly);
|
|
||||||
|
|
||||||
#if SIMPLSHARP
|
|
||||||
CType[] types;
|
|
||||||
#else
|
|
||||||
Type[] types;
|
|
||||||
#endif
|
|
||||||
try
|
|
||||||
{
|
|
||||||
types = assembly.GetTypes();
|
|
||||||
}
|
|
||||||
#if STANDARD
|
|
||||||
catch (ReflectionTypeLoadException e)
|
|
||||||
{
|
|
||||||
foreach (Exception inner in e.LoaderExceptions)
|
|
||||||
{
|
|
||||||
if (inner is System.IO.FileNotFoundException)
|
|
||||||
{
|
|
||||||
Logger.AddEntry(eSeverity.Error,
|
|
||||||
"{0} failed to cache assembly {1} - Could not find one or more dependencies by path",
|
|
||||||
typeof(AttributeUtils).Name, assembly.GetName().Name);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.AddEntry(eSeverity.Error, inner, "{0} failed to cache assembly {1}", typeof(AttributeUtils).Name,
|
|
||||||
assembly.GetName().Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
catch (TypeLoadException e)
|
|
||||||
{
|
|
||||||
#if SIMPLSHARP
|
|
||||||
Logger.AddEntry(eSeverity.Error, e, "{0} failed to cache assembly {1}", typeof(AttributeUtils).Name,
|
|
||||||
assembly.GetName().Name);
|
|
||||||
#else
|
|
||||||
Logger.AddEntry(eSeverity.Error, e, "{0} failed to cache assembly {1} - could not load type {2}",
|
|
||||||
typeof(AttributeUtils).Name, assembly.GetName().Name, e.TypeName);
|
|
||||||
#endif
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var type in types)
|
|
||||||
CacheType(type);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Pre-emptively caches the given type for lookup.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type"></param>
|
|
||||||
#if SIMPLSHARP
|
|
||||||
public static void CacheType(CType type)
|
|
||||||
#else
|
|
||||||
public static void CacheType(Type type)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
if (type == null)
|
|
||||||
throw new ArgumentNullException("type");
|
|
||||||
|
|
||||||
if (s_CachedTypes.Contains(type))
|
|
||||||
return;
|
|
||||||
|
|
||||||
s_CachedTypes.Add(type);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
s_TypeToAttributesCache[type] = new IcdHashSet<Attribute>(type.GetCustomAttributes<Attribute>(false));
|
|
||||||
foreach (Attribute attribute in s_TypeToAttributesCache[type])
|
|
||||||
s_AttributeToTypeCache[attribute] = type;
|
|
||||||
}
|
|
||||||
// GetMethods for Open Generic Types is not supported.
|
|
||||||
catch (NotSupportedException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
// Not sure why this happens :/
|
|
||||||
catch (InvalidProgramException)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Lookup
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the class attributes of the given generic type.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T"></typeparam>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IEnumerable<T> GetClassAttributes<T>()
|
|
||||||
{
|
|
||||||
return s_AttributeToTypeCache.Select(kvp => kvp.Key)
|
|
||||||
.OfType<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the first attribute on the given class type matching the generic type.
|
/// Gets the first attribute on the given class type matching the generic type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -162,56 +26,29 @@ namespace ICD.Common.Utils
|
|||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
public static T GetClassAttribute<T>(Type type)
|
public static T GetClassAttribute<T>(Type type)
|
||||||
{
|
{
|
||||||
if (type == null)
|
return GetClassAttribute<T>(type, false);
|
||||||
throw new ArgumentNullException("type");
|
|
||||||
|
|
||||||
return GetClassAttributes<T>(type).FirstOrDefault();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the attributes on the given class type matching the generic type.
|
/// Gets the first attribute on the given class type matching the generic type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="type"></param>
|
/// <param name="type"></param>
|
||||||
|
/// <param name="inherit"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IEnumerable<T> GetClassAttributes<T>(Type type)
|
[CanBeNull]
|
||||||
|
public static T GetClassAttribute<T>(Type type, bool inherit)
|
||||||
{
|
{
|
||||||
if (type == null)
|
if (type == null)
|
||||||
throw new ArgumentNullException("type");
|
throw new ArgumentNullException("type");
|
||||||
|
|
||||||
return GetClassAttributes(type).OfType<T>();
|
// ReSharper disable InvokeAsExtensionMethod
|
||||||
}
|
return ReflectionExtensions.GetCustomAttributes<T>(
|
||||||
|
#if SIMPLSHARP
|
||||||
/// <summary>
|
(CType)
|
||||||
/// Gets the attributes on the given class.
|
#endif
|
||||||
/// </summary>
|
type, inherit).FirstOrDefault();
|
||||||
/// <param name="type"></param>
|
// ReSharper restore InvokeAsExtensionMethod
|
||||||
/// <returns></returns>
|
|
||||||
public static IEnumerable<Attribute> GetClassAttributes(Type type)
|
|
||||||
{
|
|
||||||
if (type == null)
|
|
||||||
throw new ArgumentNullException("type");
|
|
||||||
|
|
||||||
CacheType(type);
|
|
||||||
|
|
||||||
IcdHashSet<Attribute> attributes;
|
|
||||||
|
|
||||||
return s_TypeToAttributesCache.TryGetValue(type, out attributes)
|
|
||||||
? attributes.ToArray(attributes.Count)
|
|
||||||
: Enumerable.Empty<Attribute>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the type with the given attribute.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="attribute"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static Type GetClass(Attribute attribute)
|
|
||||||
{
|
|
||||||
if (attribute == null)
|
|
||||||
throw new ArgumentNullException("attribute");
|
|
||||||
|
|
||||||
return s_AttributeToTypeCache[attribute];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -237,7 +74,5 @@ namespace ICD.Common.Utils
|
|||||||
.Where(p => ReflectionExtensions.GetCustomAttributes<T>(p, inherit).Any());
|
.Where(p => ReflectionExtensions.GetCustomAttributes<T>(p, inherit).Any());
|
||||||
// ReSharper restore InvokeAsExtensionMethod
|
// ReSharper restore InvokeAsExtensionMethod
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IEnumerable<T> GetCustomAttributes<T>(this ICustomAttributeProvider extends)
|
public static IEnumerable<T> GetCustomAttributes<T>(this ICustomAttributeProvider extends)
|
||||||
where T : Attribute
|
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -59,7 +58,6 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends)
|
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends)
|
||||||
where T : Attribute
|
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -75,7 +73,6 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="inherits"></param>
|
/// <param name="inherits"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends, bool inherits)
|
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends, bool inherits)
|
||||||
where T : Attribute
|
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|||||||
Reference in New Issue
Block a user