mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-12 03:05:06 +00:00
GetCustomAttribute/s extension methods
This commit is contained in:
@@ -14,6 +14,21 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
public static class ReflectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the custom attributes attached to the member.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> GetCustomAttributes<T>(this ICustomAttributeProvider extends)
|
||||
where T : Attribute
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.GetCustomAttributes<T>(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the custom attributes attached to the member.
|
||||
/// </summary>
|
||||
@@ -29,5 +44,36 @@ namespace ICD.Common.Utils.Extensions
|
||||
|
||||
return extends.GetCustomAttributes(typeof(T), inherits).Cast<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the custom attribute attached to the member.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends)
|
||||
where T : Attribute
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.GetCustomAttribute<T>(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the custom attribute attached to the member.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="inherits"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends, bool inherits)
|
||||
where T : Attribute
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.GetCustomAttributes<T>(inherits).First();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
using ICD.Common.Utils.IO;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.CrestronIO;
|
||||
@@ -251,9 +252,6 @@ namespace ICD.Common.Utils
|
||||
if (assembly == null)
|
||||
throw new ArgumentNullException("assembly");
|
||||
|
||||
#if SIMPLSHARP
|
||||
return assembly.GetCustomAttributes(typeof(T), false).Cast<T>();
|
||||
#else
|
||||
try
|
||||
{
|
||||
return assembly.GetCustomAttributes<T>();
|
||||
@@ -262,7 +260,6 @@ namespace ICD.Common.Utils
|
||||
{
|
||||
return Enumerable.Empty<T>();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user