@@ -133,7 +133,8 @@ protected static Expression BinaryExpression(IEnumerable<Expression> expressions
133133 return expressions . Aggregate ( methodExp ) ;
134134 }
135135
136- static readonly Regex _regexIndexed = new Regex ( @"(\w+)\[(\d+)\]" , RegexOptions . Compiled ) ;
136+ private static readonly Regex _regexIndexed =
137+ new Regex ( @"(?'Collection'\w+)\[(?:(?'Index'\d+)|(?:['""](?'Key'\w+)[""']))\]" , RegexOptions . Compiled ) ;
137138
138139 private static Expression GetProperty ( Expression param , string propname )
139140 {
@@ -146,26 +147,38 @@ private static Expression GetProperty(Expression param, string propname)
146147 var isIndexed = _regexIndexed . Match ( childprop ) ;
147148 if ( isIndexed . Success )
148149 {
149- var collectionname = isIndexed . Groups [ 1 ] . Value ;
150- var index = Int32 . Parse ( isIndexed . Groups [ 2 ] . Value ) ;
150+ var indexType = typeof ( int ) ;
151+ var collectionname = isIndexed . Groups [ "Collection" ] . Value ;
151152 var collectionProp = propertyType . GetProperty ( collectionname ) ;
152153 if ( collectionProp == null )
153- throw new RulesException (
154- $ "Cannot find collection property { collectionname } in class { propertyType . Name } (\" { propname } \" )") ;
154+ throw new RulesException (
155+ $ "Cannot find collection property { collectionname } in class { propertyType . Name } (\" { propname } \" )") ;
155156 var collexpr = Expression . PropertyOrField ( propExpression , collectionname ) ;
156157
158+ Expression expIndex ;
159+ if ( isIndexed . Groups [ "Index" ] . Success )
160+ {
161+ var index = Int32 . Parse ( isIndexed . Groups [ "Index" ] . Value ) ;
162+ expIndex = Expression . Constant ( index ) ;
163+ }
164+ else
165+ {
166+ expIndex = Expression . Constant ( isIndexed . Groups [ "Key" ] . Value ) ;
167+ indexType = typeof ( string ) ;
168+ }
169+
157170 var collectionType = collexpr . Type ;
158171 if ( collectionType . IsArray )
159172 {
160- propExpression = Expression . ArrayAccess ( collexpr , Expression . Constant ( index ) ) ;
173+ propExpression = Expression . ArrayAccess ( collexpr , expIndex ) ;
161174 propertyType = propExpression . Type ;
162175 }
163176 else
164177 {
165- var getter = collectionType . GetMethod ( "get_Item" , new Type [ ] { typeof ( Int32 ) } ) ;
178+ var getter = collectionType . GetMethod ( "get_Item" , new Type [ ] { indexType } ) ;
166179 if ( getter == null )
167180 throw new RulesException ( $ "'{ collectionname } ({ collectionType . Name } ) cannot be indexed") ;
168- propExpression = Expression . Call ( collexpr , getter , Expression . Constant ( index ) ) ;
181+ propExpression = Expression . Call ( collexpr , getter , expIndex ) ;
169182 propertyType = getter . ReturnType ;
170183 }
171184 }
@@ -541,7 +554,7 @@ public static bool IsSimpleType(Type type)
541554 ;
542555 }
543556 public static BindingFlags flags = BindingFlags . Instance | BindingFlags . Public ;
544- public static List < Member > GetFields ( System . Type type , string memberName = null , string parentPath = null )
557+ public static List < Member > GetFields ( Type type , string memberName = null , string parentPath = null )
545558 {
546559 List < Member > toReturn = new List < Member > ( ) ;
547560 var fi = new Member
@@ -624,7 +637,7 @@ public static bool IsGenericList(Type type)
624637 mreOperator . IsDouble . ToString ( "g" ) ,
625638 mreOperator . IsDecimal . ToString ( "g" )
626639 } ;
627- public static List < Operator > Operators ( System . Type type , bool addLogicOperators = false , bool noOverloads = true )
640+ public static List < Operator > Operators ( Type type , bool addLogicOperators = false , bool noOverloads = true )
628641 {
629642 List < Operator > operators = new List < Operator > ( ) ;
630643 if ( addLogicOperators )
0 commit comments