@@ -16,6 +16,8 @@ internal interface IModelFieldType
1616 bool HasMultipleValues { get ; }
1717 bool IsBoolean { get ; }
1818 Type BaseType { get ; }
19+ bool IsFlagsEnum { get ; }
20+ bool IsEnumerable { get ; }
1921 }
2022
2123 internal class ModelFieldType : IModelFieldType
@@ -34,7 +36,7 @@ public ModelFieldType(Type fieldType, string format)
3436 public object GetValueFromString ( string stringValue )
3537 {
3638 var value = GetUnderlyingValueFromString ( stringValue ) ;
37- if ( ! HasMultipleValues )
39+ if ( ! IsEnumerable )
3840 return value ;
3941
4042 return Cast ( new [ ] { value } ) ;
@@ -48,6 +50,19 @@ public object GetValueFromStrings(IEnumerable<string> stringValues)
4850 if ( ! HasMultipleValues )
4951 return GetUnderlyingValueFromString ( string . Join ( "," , stringValues . Where ( s => ! string . IsNullOrEmpty ( s ) ) ) ) ;
5052
53+ if ( IsFlagsEnum )
54+ {
55+ var value = Enum . Parse ( UnderlyingType , stringValues
56+ . Where ( v => ! string . IsNullOrEmpty ( v ) )
57+ . Select ( v => Convert . ToInt64 ( Enum . Parse ( UnderlyingType , v ) ) )
58+ . Aggregate ( 0L , ( x , y ) => x | y ) . ToString ( ) ) ;
59+
60+ if ( Convert . ToInt64 ( value ) == 0L && Nullable . GetUnderlyingType ( BaseType ) != null )
61+ return null ;
62+
63+ return value ;
64+ }
65+
5166 return Cast ( stringValues . Where ( s => ! string . IsNullOrEmpty ( s ) ) . Select ( GetUnderlyingValueFromString ) ) ;
5267 }
5368
@@ -94,11 +109,25 @@ public Type UnderlyingType
94109 }
95110
96111 public bool HasMultipleValues
112+ {
113+ get { return IsEnumerable || IsFlagsEnum ; }
114+ }
115+
116+ public bool IsFlagsEnum
117+ {
118+ get
119+ {
120+ return UnderlyingType . IsEnum
121+ && UnderlyingType . GetCustomAttributes ( typeof ( FlagsAttribute ) , false ) . Any ( ) ;
122+ }
123+ }
124+
125+ public bool IsEnumerable
97126 {
98127 get
99128 {
100129 return _fieldType . IsGenericType &&
101- typeof ( IEnumerable < > ) . IsAssignableFrom ( _fieldType . GetGenericTypeDefinition ( ) ) ;
130+ typeof ( IEnumerable < > ) . IsAssignableFrom ( _fieldType . GetGenericTypeDefinition ( ) ) ;
102131 }
103132 }
104133
@@ -111,7 +140,10 @@ public Type BaseType
111140 {
112141 get
113142 {
114- return HasMultipleValues ? _fieldType . GetGenericArguments ( ) [ 0 ] : _fieldType ;
143+ if ( _fieldType . IsGenericType &&
144+ typeof ( IEnumerable < > ) . IsAssignableFrom ( _fieldType . GetGenericTypeDefinition ( ) ) )
145+ return _fieldType . GetGenericArguments ( ) [ 0 ] ;
146+ return _fieldType ;
115147 }
116148 }
117149 }
0 commit comments