From 838f7fca10f3230bface975e1f61004e9d3971c3 Mon Sep 17 00:00:00 2001 From: David Hall Date: Sun, 24 Feb 2019 18:36:37 -0700 Subject: [PATCH] Added method to lookup enum value from corresponding type. --- Core/InteropServices/CorrespondingTypeAttribute.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Core/InteropServices/CorrespondingTypeAttribute.cs b/Core/InteropServices/CorrespondingTypeAttribute.cs index 16b5b7a6..23e9334a 100644 --- a/Core/InteropServices/CorrespondingTypeAttribute.cs +++ b/Core/InteropServices/CorrespondingTypeAttribute.cs @@ -71,6 +71,27 @@ namespace Vanara.InteropServices /// true if this type can get the specified reference type; otherwise, false. public static bool CanGet(Type type, Type typeRef) => GetAttrForType(type).Any(a => a.Action.IsFlagSet(CorrepsondingAction.Get) && a.TypeRef == typeRef); + /// Determines whether an enum value exists that supports a corresponding type of . + /// The corresponding type to look for. + /// The type of the enum. + /// The value of type that has the corresponding type . + /// + /// if this instance can get the specified value; otherwise, . + /// + public static bool CanGet(out TEnum value) where TEnum : struct, System.Enum + { + foreach (TEnum v in Enum.GetValues(typeof(TEnum))) + { + if (CanGet(v, typeof(T))) + { + value = v; + return true; + } + } + value = default; + return false; + } + /// Determines whether this instance can set the type for the specified enum value or class. /// The enumeration value or class instance. /// The type supplied by the user to validate.