GetCustomAttribute/s extension methods

This commit is contained in:
Chris Cameron
2018-02-07 16:56:18 -05:00
parent 232a52d608
commit 1ac1ac2965
2 changed files with 47 additions and 4 deletions

View File

@@ -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();
}
}
}

View File

@@ -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>