Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. Public Enum Currency
  2. <XmlEnum("00")> CDN = 1
  3. <XmlEnum("01")> USA= 2
  4. <XmlEnum("02")> EUR= 3
  5. <XmlEnum("03")> JPN= 4
  6. End Enum
  7.  
  8. Public Function GetXmlAttrNameFromEnumValue(Of T)(ByVal pEnumVal As T) As String
  9.  
  10. Dim type As Type = pEnumVal.GetType
  11. Dim info As FieldInfo = type.GetField([Enum].GetName(GetType(T), pEnumVal))
  12. Dim att As XmlEnumAttribute = CType(info.GetCustomAttributes(GetType(XmlEnumAttribute), False)(0), XmlEnumAttribute) 'If there is an xmlattribute defined, return the name
  13.  
  14. Return att.Name
  15. End Function
  16.  
  17. Public Shared Function GetCode(Of T)(ByVal value As String) As T
  18. For Each o As Object In System.Enum.GetValues(GetType(T))
  19. Dim enumValue As T = CType(o, T)
  20. If GetXmlAttrNameFromEnumValue(Of T)(enumValue).Equals(value, StringComparison.OrdinalIgnoreCase) Then
  21. Return CType(o, T)
  22. End If
  23. Next
  24.  
  25. Throw New ArgumentException("No code exists for type " + GetType(T).ToString() + " corresponding to value of " + value)
  26. End Function
  27.  
  28. public static string GetXmlAttrNameFromEnumValue<T>(T pEnumVal)
  29. {
  30. // http://stackoverflow.com/q/3047125/194717
  31. Type type = pEnumVal.GetType();
  32. FieldInfo info = type.GetField(Enum.GetName(typeof(T), pEnumVal));
  33. XmlEnumAttribute att = (XmlEnumAttribute)info.GetCustomAttributes(typeof(XmlEnumAttribute), false)[0];
  34. //If there is an xmlattribute defined, return the name
  35.  
  36. return att.Name;
  37. }
  38. public static T GetCode<T>(string value)
  39. {
  40. // http://stackoverflow.com/a/3073272/194717
  41. foreach (object o in System.Enum.GetValues(typeof(T)))
  42. {
  43. T enumValue = (T)o;
  44. if (GetXmlAttrNameFromEnumValue<T>(enumValue).Equals(value, StringComparison.OrdinalIgnoreCase))
  45. {
  46. return (T)o;
  47. }
  48. }
  49.  
  50. throw new ArgumentException("No XmlEnumAttribute code exists for type " + typeof(T).ToString() + " corresponding to value of " + value);
  51. }
  52.  
  53. public static class Enums
  54. {
  55. public static T GetCode<T>(string value)
  56. {
  57. foreach (object o in System.Enum.GetValues(typeof(T)))
  58. {
  59. if (((Enum)o).GetStringValue().Equals(value, StringComparison.OrdinalIgnoreCase))
  60. return (T)o;
  61. }
  62. throw new ArgumentException("No code exists for type " + typeof(T).ToString() + " corresponding to value of " + value);
  63. }
  64. }
  65.  
  66. public static string ToString2 (this Enum e) {
  67. // Get the Type of the enum
  68. Type t = e.GetType ();
  69.  
  70. // Get the FieldInfo for the member field with the enums name
  71. FieldInfo info = t.GetField (e.ToString ("G"));
  72.  
  73. // Check to see if the XmlEnumAttribute is defined on this field
  74. if (!info.IsDefined (typeof (XmlEnumAttribute), false)) {
  75. // If no XmlEnumAttribute then return the string version of the enum.
  76. return e.ToString ("G");
  77. }
  78.  
  79. // Get the XmlEnumAttribute
  80. object[] o = info.GetCustomAttributes (typeof (XmlEnumAttribute), false);
  81. XmlEnumAttribute att = (XmlEnumAttribute)o[0];
  82. return att.Name;
  83. }
  84.  
  85. /// <summary>
  86. /// Generates a dictionary allowing you to get the csharp enum value
  87. /// from the string value in the matching XmlEnumAttribute.
  88. /// You need this to be able to dynamically set entries from a xsd:enumeration
  89. /// when you've used xsd.exe to generate a .cs from the xsd.
  90. /// https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx
  91. /// </summary>
  92. /// <typeparam name="T">The xml enum type you want the mapping for</typeparam>
  93. /// <returns>Mapping dictionary from attribute values (key) to the actual enum values</returns>
  94. /// <exception cref="System.ArgumentException">T must be an enum</exception>
  95. private static Dictionary<string, T> GetEnumMap<T>() where T : struct, IConvertible
  96. {
  97. if (!typeof(T).IsEnum)
  98. {
  99. throw new ArgumentException("T must be an enum");
  100. }
  101. var members = typeof(T).GetMembers();
  102. var map = new Dictionary<string, T>();
  103. foreach (var member in members)
  104. {
  105. var enumAttrib = member.GetCustomAttributes(typeof(XmlEnumAttribute), false).FirstOrDefault() as XmlEnumAttribute;
  106. if (enumAttrib == null)
  107. {
  108. continue;
  109. }
  110. var xmlEnumValue = enumAttrib.Name;
  111. var enumVal = ((FieldInfo)member).GetRawConstantValue();
  112. map.Add(xmlEnumValue, (T)enumVal);
  113. }
  114. return map;
  115. }
  116.  
  117. var map = GetEnumMap<Currency>();
  118. return map["02"]; // returns Currency.EUR
  119.  
  120. private static T GetEnumValueFromXmlAttrName<T>(string attribVal)
  121. {
  122. T val = default(T);
  123.  
  124. if (typeof(T).BaseType.FullName.Equals("System.Enum"))
  125. {
  126. FieldInfo[] fields = typeof(T).GetFields();
  127.  
  128. foreach (FieldInfo field in fields)
  129. {
  130. object[] attribs = field.GetCustomAttributes(typeof(XmlEnumAttribute), false);
  131.  
  132. foreach (object attr in attribs)
  133. {
  134. if ((attr as XmlEnumAttribute).Name.Equals(attribVal))
  135. {
  136. val = (T)field.GetValue(null);
  137. return val;
  138. }
  139. }
  140. }
  141. }
  142. else
  143. throw new Exception("The supplied type is not an Enum.");
  144.  
  145. return val;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement