基于XmlEnumAttribute名值检索枚举值 [英] Retrieve enum value based on XmlEnumAttribute name value
问题描述
我需要一个泛型函数来获取基础上,XmlEnumAttribute名称枚举的属性的枚举的名称或值。例如,我有以下枚举定义的:
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
第一个货币枚举值是1;枚举名称为CDN和XMLEnumAttribute Name属性值是00
The first Currency enum value is 1; the enum name is "CDN"; and the XMLEnumAttribute Name property value is "00".
如果我有枚举值,我可以使用下面的泛型函数检索XmlEnumAttribute名称值:
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
因此,使用上述功能,我可以指定货币枚举类型,传递值1,和返回值将是00。
So using the above function, I can specify the Currency enum type, pass a value of 1, and the return value will be "00".
我所需要的是如果对面来执行的功能。如果我有XmlEnumAttribute名称值00,我需要一个函数返回一个货币与枚举1.正如有用的值将是将返回枚举名CDN的功能。那么我可以简单地解析此得到枚举值。
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.
推荐答案
要解决这个相同的问题的要求,使我这个问题和答案。正如我在VB.NET开发,我改写了长实的解决方案为VB和修改,使其能够使用 GetXmlAttrNameFromEnumValue
功能。
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#版本:
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);
}
这篇关于基于XmlEnumAttribute名值检索枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!