![]() Such comparisons should be done with string.Compare instead, or the string.Equals overload that takes a StringComparison. The main problem is that you're using ToUpper() to compare strings, which is easily broken (think globalization). The deserialization part is awkwardly written. The part you're using to serialize is fine. Just want to get feedback before I intergrate it into my personal framework permanently. What do you think? Overkill? Is there a better way? or Goldie Locks (just right)? Throw new ArgumentException("No code exists for type " + typeof(T).ToString() + " corresponding to value of " + value) Īnd Finally, for the UI, the GetDisplayText() is used to show the user friendly text. If (((Enum)o).GetStringValue() = value.ToUpper()) There is another method that can get Enum Value from StringValue for Deserialization: car.RoofStyle = Enums.GetCode(EDIString.Substring(4, 3))įoreach (object o in (typeof(T))) ![]() There is a custom EDI serializer that serializes based on the StringValue attributes like so: StringBuilder sb = new StringBuilder() Return attribs.Length > 0 ? attribs.DisplayText : value.ToString() Typeof(DisplayTextAttribute), false) as DisplayTextAttribute Public static string GetDisplayText(this Enum value)ĭisplayTextAttribute attribs = fieldInfo.GetCustomAttributes( ![]() Return attribs.Length > 0 ? attribs.StringValue : null Return the first if there was a match. Typeof(StringValueAttribute), false) as StringValueAttribute StringValueAttribute attribs = fieldInfo.GetCustomAttributes( The Attributes are accessed via Extension methods: public static string GetStringValue(this Enum value)įieldInfo fieldInfo = type.GetField(value.ToString()) I have many Enums like the following with custom attributes applied. A car might not be the best example b/c options change over time, but for the real object the Enums will never change. I have an object that needs to be serialized to an EDI format.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |