From b4dde29d90b15ca20698bd950432bc39052b4d14 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 24 Jul 2019 12:55:23 -0600 Subject: [PATCH] Simplified and extended GetCorrespondingTypes methods --- Core/InteropServices/CorrespondingTypeAttribute.cs | 29 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Core/InteropServices/CorrespondingTypeAttribute.cs b/Core/InteropServices/CorrespondingTypeAttribute.cs index d60836c9..f9ab78fe 100644 --- a/Core/InteropServices/CorrespondingTypeAttribute.cs +++ b/Core/InteropServices/CorrespondingTypeAttribute.cs @@ -115,15 +115,34 @@ namespace Vanara.InteropServices public static bool CanSet(Type type, Type typeRef) => GetAttrForType(type).Any(a => a.Action.IsFlagSet(CorrespondingAction.Set) && a.TypeRef == typeRef); /// Gets the corresponding types for the supplied enumeration value. - /// The enumeration value or class. + /// The class or structure instance. /// The types defined by the attribute. public static IEnumerable GetCorrespondingTypes(object enumValue) => GetAttrForObj(enumValue).Select(a => a.TypeRef); /// Gets the corresponding types for the supplied enumeration value. - /// The enumeration value or class. + /// The enumeration value. + /// The types defined by the attribute. + public static IEnumerable GetCorrespondingTypes(TEnum enumValue) where TEnum : System.Enum => GetAttrForEnum(enumValue).Select(a => a.TypeRef); + + /// Gets the corresponding types for the supplied enumeration value. + /// The enumeration value. + /// The action to filter for. + /// The types defined by the attribute. + public static IEnumerable GetCorrespondingTypes(TEnum enumValue, CorrespondingAction action) where TEnum : System.Enum => GetAttrForEnum(enumValue, action).Select(a => a.TypeRef); + + /// Gets the corresponding types for the supplied enumeration value. + /// The class type. /// The types defined by the attribute. public static IEnumerable GetCorrespondingTypes(Type type) => GetAttrForType(type).Select(a => a.TypeRef); + /// Gets the CorrespondingTypeAttribute instances associated with an enum value. + /// The type of the enum. + /// The enum value. + /// The action to filter for. + /// An enumeration of all associated CorrespondingTypeAttribute instances. + protected static IEnumerable GetAttrForEnum(TEnum value, CorrespondingAction action) where TEnum : System.Enum => + GetAttrForEnum(value).Where(a => a.Action == action); + /// Gets the CorrespondingTypeAttribute instances associated with an enum value. /// The enum value. /// An enumeration of all associated CorrespondingTypeAttribute instances. @@ -136,15 +155,15 @@ namespace Vanara.InteropServices return attr; } - /// Gets the CorrespondingTypeAttribute instances associated with an enum value. - /// The enum value. + /// Gets the CorrespondingTypeAttribute instances associated with an enum value or class instance. + /// The enum value or class instance. /// An enumeration of all associated CorrespondingTypeAttribute instances. protected static IEnumerable GetAttrForObj(object value) { if (value is 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() : valueType.GetCustomAttributes()); + var attr = valueType.IsEnum ? valueType.GetField(value.ToString()).GetCustomAttributes() : valueType.GetCustomAttributes(); if (!attr.Any()) return new CorrespondingTypeAttribute[0]; if (attr.Any(a => a.Action == CorrespondingAction.Exception)) throw new Exception(); return attr;