public PluralEntry(IEnumerable <string> values, MatchTypeOptions matchType) { if (values == null) { throw new ArgumentNullException("values"); } if (!Enum.IsDefined(typeof(MatchTypeOptions), matchType)) { throw new ArgumentOutOfRangeException("matchType"); } var valuesTidied = TidyStringList(values, v => v.Trim().ToLower()); if (!valuesTidied.Any()) { throw new ArgumentException("No entries in values set"); } Values = valuesTidied.AsReadOnly(); MatchType = matchType; }
/// <summary> /// Generate an expression that determines whether a string parameter matches a specified suffix / matchType combination /// </summary> private static Expression GeneratePredicate(string suffix, ParameterExpression valueTrimmed, MatchTypeOptions matchType) { if (string.IsNullOrWhiteSpace(suffix)) { throw new ArgumentException("Null/blank suffix specified"); } if (valueTrimmed == null) { throw new ArgumentNullException("valueTrimmed"); } if (!Enum.IsDefined(typeof(MatchTypeOptions), matchType)) { throw new ArgumentOutOfRangeException("matchType"); } suffix = suffix.Trim(); var conditionElements = new List <Expression>(); var lengthProperty = typeof(string).GetProperty("Length"); var indexedProperty = typeof(string).GetProperties().First(p => (p.GetIndexParameters() ?? new ParameterInfo[0]).Any()); if (matchType == MatchTypeOptions.SuffixOnly) { conditionElements.Add( Expression.GreaterThan( Expression.Property(valueTrimmed, lengthProperty), Expression.Constant(suffix.Length, typeof(int)) ) ); } else { conditionElements.Add( Expression.Equal( Expression.Property(valueTrimmed, lengthProperty), Expression.Constant(suffix.Length, typeof(int)) ) ); } for (var index = 0; index < suffix.Length; index++) { conditionElements.Add( Expression.Equal( Expression.Constant(suffix[index], typeof(char)), Expression.Property( valueTrimmed, indexedProperty, Expression.Subtract( Expression.Property(valueTrimmed, lengthProperty), Expression.Constant(suffix.Length - index, typeof(int)) ) ) ) ); } return(CombineExpressionsWithAndAlso(conditionElements)); }