mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
Caching class attributes
This commit is contained in:
@@ -25,6 +25,7 @@ namespace ICD.Common.Utils
|
|||||||
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, MethodInfo> s_AttributeToMethodCache;
|
||||||
|
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;
|
||||||
|
|
||||||
private static ILoggerService Logger { get { return ServiceProvider.TryGetService<ILoggerService>(); } }
|
private static ILoggerService Logger { get { return ServiceProvider.TryGetService<ILoggerService>(); } }
|
||||||
@@ -38,6 +39,7 @@ namespace ICD.Common.Utils
|
|||||||
s_CachedTypes = new IcdHashSet<Type>();
|
s_CachedTypes = new IcdHashSet<Type>();
|
||||||
|
|
||||||
s_AttributeToMethodCache = new Dictionary<Attribute, MethodInfo>();
|
s_AttributeToMethodCache = new Dictionary<Attribute, MethodInfo>();
|
||||||
|
s_AttributeToTypeCache = new Dictionary<Attribute, Type>();
|
||||||
s_TypeToAttributesCache = new Dictionary<Type, IcdHashSet<Attribute>>();
|
s_TypeToAttributesCache = new Dictionary<Type, IcdHashSet<Attribute>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,14 +131,11 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if SIMPLSHARP
|
|
||||||
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])
|
||||||
|
s_AttributeToTypeCache[attribute] = type;
|
||||||
|
|
||||||
methods = type.GetMethods();
|
methods = type.GetMethods();
|
||||||
#else
|
|
||||||
s_TypeToAttributesCache[type] =
|
|
||||||
new IcdHashSet<Attribute>(ReflectionExtensions.GetCustomAttributes<Attribute>(type.GetTypeInfo(), false));
|
|
||||||
methods = type.GetTypeInfo().GetMethods();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
// GetMethods for Open Generic Types is not supported.
|
// GetMethods for Open Generic Types is not supported.
|
||||||
catch (NotSupportedException)
|
catch (NotSupportedException)
|
||||||
@@ -170,6 +169,18 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
#region Lookup
|
#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>()
|
||||||
|
where T : Attribute
|
||||||
|
{
|
||||||
|
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>
|
||||||
@@ -218,6 +229,19 @@ namespace ICD.Common.Utils
|
|||||||
: Enumerable.Empty<Attribute>();
|
: 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>
|
||||||
/// Gets all of the cached method attributes of the given type.
|
/// Gets all of the cached method attributes of the given type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user