diff --git a/Core/Extensions/ReflectionExtensions.cs b/Core/Extensions/ReflectionExtensions.cs
index 956fcfae..d50df657 100644
--- a/Core/Extensions/ReflectionExtensions.cs
+++ b/Core/Extensions/ReflectionExtensions.cs
@@ -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
/// Extensions related to System.Reflection
public static class ReflectionExtensions
{
+ /// Returns an array of custom attributes applied to this member and identified by .
+ /// The type of attribute to search for. Only attributes that are assignable to this type are returned.
+ /// An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class.
+ /// true to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events.
+ /// An optional predicate to refine the results.
+ ///
+ public static IEnumerable GetCustomAttributes(this MemberInfo element, bool inherit = false, Func predicate = null) where TAttr : Attribute =>
+ element.GetCustomAttributes(typeof(TAttr), inherit).Cast().Where(predicate ?? (a => true));
+
+ /// Returns an array of custom attributes applied to this member and identified by .
+ /// The type of attribute to search for. Only attributes that are assignable to this type are returned.
+ /// The type of the to examine.
+ /// true to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events.
+ /// An optional predicate to refine the results.
+ ///
+ public static IEnumerable GetCustomAttributes(this Type type, bool inherit = false, Func predicate = null) where TAttr : Attribute =>
+ type.GetCustomAttributes(typeof(TAttr), inherit).Cast().Where(predicate ?? (a => true));
+
/// Gets a named property value from an object.
/// The expected type of the property to be returned.
/// The object from which to retrieve the property.
diff --git a/Core/InteropServices/CorrespondingTypeAttribute.cs b/Core/InteropServices/CorrespondingTypeAttribute.cs
index c731f902..5eb5c8ed 100644
--- a/Core/InteropServices/CorrespondingTypeAttribute.cs
+++ b/Core/InteropServices/CorrespondingTypeAttribute.cs
@@ -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().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() : valueType.GetCustomAttributes());
+ 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 GetAttrForType(Type type)
{
if (type == null) throw new ArgumentNullException(nameof(type));
- var attr = type.GetCustomAttributes(typeof(CorrespondingTypeAttribute), false).Cast().ToArray();
- if (attr == null || attr.Length == 0) throw new InvalidOperationException("Type must have the CorrespondingTypeAttribute defined.");
+ var attr = type.GetCustomAttributes();
+ 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;
}
diff --git a/PInvoke/Shared/AssociateAttribute.cs b/PInvoke/Shared/AssociateAttribute.cs
index f5e66104..f55d7432 100644
--- a/PInvoke/Shared/AssociateAttribute.cs
+++ b/PInvoke/Shared/AssociateAttribute.cs
@@ -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
/// An enum type.
/// The enum value.
/// The GUID.
- public static Guid GetGuidFromEnum(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 value) =>
+ typeof(T).GetField(value.ToString()).GetCustomAttributes().Select(a => a.Guid).FirstOrDefault();
/// Tries a lookup of the enum value associated with a Guid.
/// The unique identifier.
@@ -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().All(a => a.Guid != guid))
+ if (f.GetCustomAttributes().All(a => a.Guid != guid))
continue;
value = (T)f.GetRawConstantValue();
return true;
diff --git a/PInvoke/Shell32/KnownFolderIdExt.cs b/PInvoke/Shell32/KnownFolderIdExt.cs
index 89fe2bbe..8dcab6f4 100644
--- a/PInvoke/Shell32/KnownFolderIdExt.cs
+++ b/PInvoke/Shell32/KnownFolderIdExt.cs
@@ -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
/// Retrieves the associated with a if it exists.
/// The known folder.
/// The if defined, null otherwise.
- 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().Select(a => (Environment.SpecialFolder?)a.Equivalent).FirstOrDefault();
}
}
\ No newline at end of file
diff --git a/WIndows.Forms/Design/GenericDesigner.cs b/WIndows.Forms/Design/GenericDesigner.cs
index 5cd752ad..a80de6c5 100644
--- a/WIndows.Forms/Design/GenericDesigner.cs
+++ b/WIndows.Forms/Design/GenericDesigner.cs
@@ -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 attributes;
if (attr.ApplyOtherAttributes)
{
- attributes = new List(Array.ConvertAll(prop.GetCustomAttributes(false), o => o as Attribute));
+ attributes = prop.GetCustomAttributes().ToList();
attributes.RemoveAll(a => a is RedirectedDesignerPropertyAttribute);
}
else