mirror of https://github.com/dahall/Vanara.git
Added GetCustomAttributes extension method and converted existing uses.
parent
67fe9d4632
commit
e617c912a3
|
@ -1,4 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
|
||||
|
@ -7,6 +9,24 @@ namespace Vanara.Extensions
|
|||
/// <summary>Extensions related to <c>System.Reflection</c></summary>
|
||||
public static class ReflectionExtensions
|
||||
{
|
||||
/// <summary>Returns an array of custom attributes applied to this member and identified by <typeparamref name="TAttr"/>.</summary>
|
||||
/// <typeparam name="TAttr">The type of attribute to search for. Only attributes that are assignable to this type are returned.</typeparam>
|
||||
/// <param name="element">An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class.</param>
|
||||
/// <param name="inherit"><c>true</c> to search this member's inheritance chain to find the attributes; otherwise, <c>false</c>. This parameter is ignored for properties and events.</param>
|
||||
/// <param name="predicate">An optional predicate to refine the results.</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TAttr> GetCustomAttributes<TAttr>(this MemberInfo element, bool inherit = false, Func<TAttr, bool> predicate = null) where TAttr : Attribute =>
|
||||
element.GetCustomAttributes(typeof(TAttr), inherit).Cast<TAttr>().Where(predicate ?? (a => true));
|
||||
|
||||
/// <summary>Returns an array of custom attributes applied to this member and identified by <typeparamref name="TAttr"/>.</summary>
|
||||
/// <typeparam name="TAttr">The type of attribute to search for. Only attributes that are assignable to this type are returned.</typeparam>
|
||||
/// <param name="type">The type of the <see cref="Type"/> to examine.</param>
|
||||
/// <param name="inherit"><c>true</c> to search this member's inheritance chain to find the attributes; otherwise, <c>false</c>. This parameter is ignored for properties and events.</param>
|
||||
/// <param name="predicate">An optional predicate to refine the results.</param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TAttr> GetCustomAttributes<TAttr>(this Type type, bool inherit = false, Func<TAttr, bool> predicate = null) where TAttr : Attribute =>
|
||||
type.GetCustomAttributes(typeof(TAttr), inherit).Cast<TAttr>().Where(predicate ?? (a => true));
|
||||
|
||||
/// <summary>Gets a named property value from an object.</summary>
|
||||
/// <typeparam name="T">The expected type of the property to be returned.</typeparam>
|
||||
/// <param name="obj">The object from which to retrieve the property.</param>
|
||||
|
|
|
@ -106,8 +106,8 @@ namespace Vanara.InteropServices
|
|||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
var valueType = value.GetType();
|
||||
if (!valueType.IsEnum && !valueType.IsClass) throw new ArgumentException("Value must be an enumeration or class value.", nameof(value));
|
||||
var attr = (valueType.IsEnum ? valueType.GetField(value.ToString()).GetCustomAttributes(typeof(CorrespondingTypeAttribute), false) : valueType.GetCustomAttributes(typeof(CorrespondingTypeAttribute), false)).Cast<CorrespondingTypeAttribute>().ToArray();
|
||||
if (attr == null || attr.Length == 0) throw new InvalidOperationException("Value must have the CorrespondingTypeAttribute defined.");
|
||||
var attr = (valueType.IsEnum ? valueType.GetField(value.ToString()).GetCustomAttributes<CorrespondingTypeAttribute>() : valueType.GetCustomAttributes<CorrespondingTypeAttribute>());
|
||||
if (!attr.Any()) throw new InvalidOperationException("Value must have the CorrespondingTypeAttribute defined.");
|
||||
if (attr.Any(a => a.Action == CorrepsondingAction.Exception)) throw new Exception();
|
||||
return attr;
|
||||
}
|
||||
|
@ -115,8 +115,8 @@ namespace Vanara.InteropServices
|
|||
private static IEnumerable<CorrespondingTypeAttribute> GetAttrForType(Type type)
|
||||
{
|
||||
if (type == null) throw new ArgumentNullException(nameof(type));
|
||||
var attr = type.GetCustomAttributes(typeof(CorrespondingTypeAttribute), false).Cast<CorrespondingTypeAttribute>().ToArray();
|
||||
if (attr == null || attr.Length == 0) throw new InvalidOperationException("Type must have the CorrespondingTypeAttribute defined.");
|
||||
var attr = type.GetCustomAttributes<CorrespondingTypeAttribute>();
|
||||
if (!attr.Any()) throw new InvalidOperationException("Type must have the CorrespondingTypeAttribute defined.");
|
||||
if (attr.Any(a => a.Action == CorrepsondingAction.Exception)) throw new Exception();
|
||||
return attr;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Vanara.Extensions;
|
||||
|
||||
namespace Vanara.PInvoke
|
||||
{
|
||||
|
@ -26,11 +27,8 @@ namespace Vanara.PInvoke
|
|||
/// <typeparam name="T">An enum type.</typeparam>
|
||||
/// <param name="value">The enum value.</param>
|
||||
/// <returns>The GUID.</returns>
|
||||
public static Guid GetGuidFromEnum<T>(T value)
|
||||
{
|
||||
var attr = typeof(T).GetField(value.ToString()).GetCustomAttributes(typeof(AssociateAttribute), false);
|
||||
return attr.Length > 0 ? ((AssociateAttribute) attr[0]).Guid : Guid.Empty;
|
||||
}
|
||||
public static Guid GetGuidFromEnum<T>(T value) =>
|
||||
typeof(T).GetField(value.ToString()).GetCustomAttributes<AssociateAttribute>().Select(a => a.Guid).FirstOrDefault();
|
||||
|
||||
/// <summary>Tries a lookup of the enum value associated with a Guid.</summary>
|
||||
/// <param name="guid">The unique identifier.</param>
|
||||
|
@ -40,7 +38,7 @@ namespace Vanara.PInvoke
|
|||
{
|
||||
foreach (var f in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static))
|
||||
{
|
||||
if (f.GetCustomAttributes(typeof(AssociateAttribute), false).Cast<AssociateAttribute>().All(a => a.Guid != guid))
|
||||
if (f.GetCustomAttributes<AssociateAttribute>().All(a => a.Guid != guid))
|
||||
continue;
|
||||
value = (T)f.GetRawConstantValue();
|
||||
return true;
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using Vanara.Extensions;
|
||||
using static Vanara.PInvoke.AdvApi32;
|
||||
using static Vanara.PInvoke.Shell32;
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
@ -75,13 +77,7 @@ namespace Vanara.PInvoke
|
|||
/// <summary>Retrieves the <see cref="Environment.SpecialFolder"/> associated with a <see cref="KNOWNFOLDERID"/> if it exists.</summary>
|
||||
/// <param name="id">The known folder.</param>
|
||||
/// <returns>The <see cref="Environment.SpecialFolder"/> if defined, <c>null</c> otherwise.</returns>
|
||||
public static Environment.SpecialFolder? SpecialFolder(this KNOWNFOLDERID id)
|
||||
{
|
||||
var attr = typeof(KNOWNFOLDERID).GetField(id.ToString()).GetCustomAttributes(typeof(KnownFolderDetailAttribute), false);
|
||||
var ret = (Environment.SpecialFolder) 0XFFFF;
|
||||
if (attr.Length > 0)
|
||||
ret = ((KnownFolderDetailAttribute) attr[0]).Equivalent;
|
||||
return ret == (Environment.SpecialFolder) 0XFFFF ? (Environment.SpecialFolder?) null : ret;
|
||||
}
|
||||
public static Environment.SpecialFolder? SpecialFolder(this KNOWNFOLDERID id) =>
|
||||
typeof(KNOWNFOLDERID).GetField(id.ToString()).GetCustomAttributes<KnownFolderDetailAttribute>().Select(a => (Environment.SpecialFolder?)a.Equivalent).FirstOrDefault();
|
||||
}
|
||||
}
|
|
@ -4,10 +4,12 @@ using System.Collections.Generic;
|
|||
using System.ComponentModel;
|
||||
using System.ComponentModel.Design;
|
||||
using System.Drawing.Design;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Forms.Design;
|
||||
using System.Windows.Forms.Design.Behavior;
|
||||
using Vanara.Extensions;
|
||||
|
||||
namespace Vanara.Windows.Forms.Design
|
||||
{
|
||||
|
@ -721,7 +723,7 @@ namespace Vanara.Windows.Forms.Design
|
|||
List<Attribute> attributes;
|
||||
if (attr.ApplyOtherAttributes)
|
||||
{
|
||||
attributes = new List<Attribute>(Array.ConvertAll(prop.GetCustomAttributes(false), o => o as Attribute));
|
||||
attributes = prop.GetCustomAttributes<Attribute>().ToList();
|
||||
attributes.RemoveAll(a => a is RedirectedDesignerPropertyAttribute);
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue