I need a Generic function to retrieve the name or value of an enum based on the XmlEnumAttribute "Name" property of the enum. For example I have the following enum defined:
Public Enum Currency
<XmlEnum("00")> CDN = 1
<XmlEnum("01")> USA= 2
<XmlEnum("02")> EUR= 3
<XmlEnum("03")> JPN= 4
End Enum
The first Currency enum value is 1; the enum name is "CDN"; and the XMLEnumAttribute Name property value is "00".
If I have the enum value, I can retrieve the XmlEnumAttribute "Name" value using the following generic function:
Public Function GetXmlAttrNameFromEnumValue(Of T)(ByVal pEnumVal As T) As String
Dim type As Type = pEnumVal.GetType
Dim info As FieldInfo = type.GetField([Enum].GetName(GetType(T), pEnumVal))
Dim att As XmlEnumAttribute = CType(info.GetCustomAttributes(GetType(XmlEnumAttribute), False)(0), XmlEnumAttribute) 'If there is an xmlattribute defined, return the name
Return att.Name
End Function
So using the above function, I can specify the Currency enum type, pass a value of 1, and the return value will be "00".
What I need is a function to perform if the opposite. If I have the XmlEnumAttribute Name value "00", I need a function to return a Currency enum with a value of 1. Just as useful would be a function that would return the enum name "CDN". I could then simply parse this to get the enum value.
Any assistance would be appreciated.
Enum instances must obey the same java language naming restrictions as other identifiers - they can't start with a number.
A requirement to solve this exact same problem led me to this question and answer. As I develop in VB.NET, I rewrote CkH's solution into VB and modified it to use your GetXmlAttrNameFromEnumValue
function.
Public Shared Function GetCode(Of T)(ByVal value As String) As T
For Each o As Object In System.Enum.GetValues(GetType(T))
Dim enumValue As T = CType(o, T)
If GetXmlAttrNameFromEnumValue(Of T)(enumValue).Equals(value, StringComparison.OrdinalIgnoreCase) Then
Return CType(o, T)
End If
Next
Throw New ArgumentException("No code exists for type " + GetType(T).ToString() + " corresponding to value of " + value)
End Function
C# Version:
public static string GetXmlAttrNameFromEnumValue<T>(T pEnumVal)
{
// http://stackoverflow.com/q/3047125/194717
Type type = pEnumVal.GetType();
FieldInfo info = type.GetField(Enum.GetName(typeof(T), pEnumVal));
XmlEnumAttribute att = (XmlEnumAttribute)info.GetCustomAttributes(typeof(XmlEnumAttribute), false)[0];
//If there is an xmlattribute defined, return the name
return att.Name;
}
public static T GetCode<T>(string value)
{
// http://stackoverflow.com/a/3073272/194717
foreach (object o in System.Enum.GetValues(typeof(T)))
{
T enumValue = (T)o;
if (GetXmlAttrNameFromEnumValue<T>(enumValue).Equals(value, StringComparison.OrdinalIgnoreCase))
{
return (T)o;
}
}
throw new ArgumentException("No XmlEnumAttribute code exists for type " + typeof(T).ToString() + " corresponding to value of " + value);
}
I do something similar with custom attributes and I use this method to get the EnumValue based on the Attribute Value. GetStringValue is my custom method, similar to your example above.
public static class Enums
{
public static T GetCode<T>(string value)
{
foreach (object o in System.Enum.GetValues(typeof(T)))
{
if (((Enum)o).GetStringValue().Equals(value, StringComparison.OrdinalIgnoreCase))
return (T)o;
}
throw new ArgumentException("No code exists for type " + typeof(T).ToString() + " corresponding to value of " + value);
}
}
For the whole process I use check this post and the answers: Extending Enums, Overkill?
Sorry this is in C#, just realized you were using VB.NET above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With