33using System . Collections . Concurrent ;
44using System . Collections . Generic ;
55using System . Data ;
6+ using System . Reflection ;
7+ using System . Runtime . Serialization ;
68using ServiceStack . DataAnnotations ;
79#if NETSTANDARD2_0
810using System . Globalization ;
@@ -14,7 +16,8 @@ public enum EnumKind
1416 {
1517 String ,
1618 Int ,
17- Char
19+ Char ,
20+ EnumMember ,
1821 }
1922
2023 public class EnumConverter : StringConverter
@@ -32,7 +35,9 @@ public static EnumKind GetEnumKind(Type enumType)
3235 ? EnumKind . Int
3336 : enumType . HasAttributeCached < EnumAsCharAttribute > ( )
3437 ? EnumKind . Char
35- : EnumKind . String ;
38+ : HasEnumMembers ( enumType )
39+ ? EnumKind . EnumMember
40+ : EnumKind . String ;
3641
3742 Dictionary < Type , EnumKind > snapshot , newCache ;
3843 do
@@ -109,6 +114,9 @@ public override object ToDbValue(Type fieldType, object value)
109114 value = Enum . ToObject ( fieldType , enumValue ) ;
110115 }
111116
117+ if ( enumKind == EnumKind . EnumMember ) // Don't use serialized Enum Value
118+ return value . ToString ( ) ;
119+
112120 var enumString = DialectProvider . StringSerializer . SerializeToString ( value ) ;
113121 return enumString != null && enumString != "null"
114122 ? enumString . Trim ( '"' )
@@ -141,6 +149,18 @@ public static bool IsIntEnum(Type fieldType)
141149 return isIntEnum ;
142150 }
143151
152+ public static bool HasEnumMembers ( Type enumType )
153+ {
154+ var enumMembers = enumType . GetFields ( BindingFlags . Public | BindingFlags . Static ) ;
155+ foreach ( var fi in enumMembers )
156+ {
157+ var enumMemberAttr = fi . FirstAttribute < EnumMemberAttribute > ( ) ;
158+ if ( enumMemberAttr ? . Value != null )
159+ return true ;
160+ }
161+ return false ;
162+ }
163+
144164 public override object FromDbValue ( Type fieldType , object value )
145165 {
146166 var enumKind = GetEnumKind ( fieldType ) ;
0 commit comments