c# - Retrieve enum value based on XmlEnumAttribute name value -


I need normal work to retrieve the name or value of the enum based on "name" XmlEnumAttribute "name". For example, I have the following values ​​defined:

  public enum currency  

The first currency ANIM value is 1; Enum's name is "cdn"; And the name of the property named XMLEnumAttribute is "00"

If I have the value enum, I can get XmlEnumAttribute "Name" value following the following general function:.

  public function GetXmlAttrNameFromEnumValue (t) (ByVal pEnumVal As T) As String = pEnumVal.GetType as dim type Dim information FieldInfo = type.GetField ([Enum] .GetName (GetType (T), pEnumVal)) is defined as = CTYPE (info.GetCustomAttributes (GetType (XmlEnumAttribute), false) (0), XmlEnumAttribute) 'If any xmlattribute as dim att XmlEnumAttribute, Name return return att.Name termination function  

Then using the above mentioned function, I can specify the currency enum type I can pass the value of 1, and the return value will be "00".

What I want to do if there is a function to do the opposite. If I have the value of the name XmlEnumAttribute "00", then I need a function to return the currency NM with the value of 1. Just as useful would be an act that will return the enum name "CDN". Then I can parse the value for the value.

Any support will be appreciated.

A need to solve this exact same problem led me to this question and answer . As I developed in VB.NET, I rewritten the CKH solution in VB and modified it to work GetXmlAttrNameFromEnumValue to use your .

  public shared function GetCode (t) (in t o each object System.Enum.GetValues ​​(GetType (T) as the value as a string) to ByVal) dim EnumValue as T = as CTYPE (o, t) if GetXmlAttrNameFromEnumValue (t) (EnumValue) .Equals (price, StringComparison. OrdinalIgnoreCase) then return CTYPE (O, T) end of the next new ArgumentException (+ GetType (T "exists for any code type") .ToString () + value) Expiry Function  <"value corresponding to" / pre> 

C # version:

  Arwajnik static string GetXmlAttrNameFromEnumValue & LT; T & gt; (T pEnumVal) {// http://stackoverflow.com/q/3047125/194717 Type Type = pEnumVal .GetType (); Field Info Info = Type Getfield (Enum.GetName (typef (t), pimmweel)); XmlEnumAttribute at = (XmlEnumAttribute) info.GetCustomAttributes (typeof (XmlEnumAttribute), incorrect) [0]; // If any xmlattribute is defined, return the name return attribute name; } Public static T GetCode & LT; T & gt; (String value) {// http://stackoverflow.com/a/3073272/194717 foreach (o object System.Enum.GetValues ​​(typeof (T))) {T EnumValue = (served me; if (GetXmlAttrNameFromEnumValue & LT; t & gt; (EnumValue) exists for KEquals (price, StringComparison.OrdinalIgnoreCase)) {return (t) o;}} new logic expression ( "no XmlEnumAttribute code type" + Taipf (T). Tustring () + "Value-wise" + value);}  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -