using System;
using System.Collections.Generic;
using System.Linq;
using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
#if SIMPLSHARP
using Crestron.SimplSharp.Reflection;
#else
using System.Reflection;
#endif
namespace ICD.Common.Utils
{
///
/// Utility methods for browsing code attributes.
/// Provides some basic caching for faster subsequent searches.
///
public static class AttributeUtils
{
///
/// Gets the first attribute on the given class type matching the generic type.
///
///
///
///
[CanBeNull]
public static T GetClassAttribute(Type type)
{
return GetClassAttribute(type, false);
}
///
/// Gets the first attribute on the given class type matching the generic type.
///
///
///
///
///
[CanBeNull]
public static T GetClassAttribute(Type type, bool inherit)
{
if (type == null)
throw new ArgumentNullException("type");
return GetClassAttributes(type, inherit).FirstOrDefault();
}
///
/// Gets the attributes on the given class type matching the generic type.
///
///
///
///
public static IEnumerable GetClassAttributes(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
return GetClassAttributes(type, false);
}
///
/// Gets the attributes on the given class type matching the generic type.
///
///
///
///
///
public static IEnumerable GetClassAttributes(Type type, bool inherit)
{
if (type == null)
throw new ArgumentNullException("type");
// ReSharper disable InvokeAsExtensionMethod
return ReflectionExtensions.GetCustomAttributes(
#if SIMPLSHARP
(CType)
#endif
type, inherit);
// ReSharper restore InvokeAsExtensionMethod
}
///
/// Returns the properties on the given instance with property attributes of the given type.
///
///
///
///
///
public static IEnumerable GetProperties(object instance, bool inherit)
{
if (instance == null)
throw new ArgumentNullException("instance");
return instance.GetType()
#if SIMPLSHARP
.GetCType()
#else
.GetTypeInfo()
#endif
.GetProperties()
// ReSharper disable InvokeAsExtensionMethod
.Where(p => ReflectionExtensions.GetCustomAttributes(p, inherit).Any());
// ReSharper restore InvokeAsExtensionMethod
}
}
}