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