private Expression GetSafeStringExpression(Expression member, eBooleanOperatorType operation, Expression constant, Expression constant2) { if (member.Type != typeof(string)) { return(Expressions[operation].Invoke(member, constant, constant2)); } Expression newMember = member; if (operation != eBooleanOperatorType.IsNullOrWhiteSpace && operation != eBooleanOperatorType.IsNotNullNorWhiteSpace) { var trimMemberCall = Expression.Call(member, trimMethod); newMember = Expression.Call(trimMemberCall, toLowerMethod); } Expression resultExpr = operation != eBooleanOperatorType.IsNull ? Expressions[operation].Invoke(newMember, constant, constant2) : Expressions[operation].Invoke(member, constant, constant2); if (member.Type == typeof(string) && operation != eBooleanOperatorType.IsNull) { if (operation != eBooleanOperatorType.IsNullOrWhiteSpace && operation != eBooleanOperatorType.IsNotNullNorWhiteSpace) { Expression memberIsNotNull = Expression.NotEqual(member, Expression.Constant(null)); resultExpr = Expression.AndAlso(memberIsNotNull, resultExpr); } } return(resultExpr); }
public FilterStatement(string propertyId, eBooleanOperatorType operation, Enum value, Enum value2 = default(Enum), eLogicOperatorType connector = eLogicOperatorType.And) { PropertyName = propertyId; LogicOperator = connector; BooleanOperator = operation; Value = value; Value2 = value2; TypeGroups = new Dictionary <TypeGroup, HashSet <Type> > { { TypeGroup.Text, new HashSet <Type> { typeof(string), typeof(char) } }, { TypeGroup.Number, new HashSet <Type> { typeof(int), typeof(uint), typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(long), typeof(ulong), typeof(Single), typeof(double), typeof(decimal) } }, { TypeGroup.Boolean, new HashSet <Type> { typeof(bool) } }, { TypeGroup.Date, new HashSet <Type> { typeof(DateTime) } }, { TypeGroup.Nullable, new HashSet <Type> { typeof(Nullable <>) } } }; Validate(); }
public int NumberOfValuesAcceptable(eBooleanOperatorType operation) { var fieldInfo = operation.GetType().GetField(operation.ToString()); var attrs = fieldInfo.GetCustomAttributes(false); var attr = attrs.FirstOrDefault(a => a is NumberOfValuesAttribute); return((attr as NumberOfValuesAttribute).NumberOfValues); }
private static void GetValue(PropertyInfo valProperty, EntityFilter valItem, eBooleanOperatorType valBooleanOperatorType, ref object valValue1, ref object valValue2) { var vType = Nullable.GetUnderlyingType(valProperty.PropertyType) ?? valProperty.PropertyType; int vNumberOfValuesAcceptable = new FilterStatement <string>().NumberOfValuesAcceptable(valBooleanOperatorType); if (vNumberOfValuesAcceptable > 0) { if (vType == typeof(DateTime)) { valValue1 = Convert.ToDateTime(valValue1); } else if (vType == typeof(int)) { valValue1 = Convert.ToInt32(valValue1); } else if (vType == typeof(decimal)) { valValue1 = Convert.ToDecimal(valValue1); } else if (vType == typeof(bool)) { valValue1 = Convert.ToBoolean(valItem.Value3); } else if (vType.IsEnum) { valValue1 = Enum.ToObject(vType, Convert.ToInt32(valItem.Value3)); } } else { valValue1 = null; } if (vNumberOfValuesAcceptable == 2) { if (vType == typeof(DateTime)) { valValue2 = Convert.ToDateTime(valValue2); } else if (vType == typeof(int)) { valValue2 = Convert.ToInt32(valValue2); } else if (vType == typeof(decimal)) { valValue2 = Convert.ToDecimal(valValue2); } } else { valValue2 = null; } }
public FilterStatement(string propertyId, eBooleanOperatorType operation, TPropertyType value, TPropertyType value2 = default(TPropertyType), eLogicOperatorType connector = eLogicOperatorType.And) { PropertyName = propertyId; LogicOperator = connector; BooleanOperator = operation; if (typeof(TPropertyType).IsArray) { if (operation != eBooleanOperatorType.Contains && operation != eBooleanOperatorType.In) { throw new ArgumentException("Only 'Operacao.Contains' and 'Operacao.In' support arrays as parameters."); } var listType = typeof(List <>); var constructedListType = listType.MakeGenericType(typeof(TPropertyType).GetElementType()); Value = value != null?Activator.CreateInstance(constructedListType, value) : null; Value2 = value2 != null?Activator.CreateInstance(constructedListType, value2) : null; } else { Value = value; Value2 = value2; } TypeGroups = new Dictionary <TypeGroup, HashSet <Type> > { { TypeGroup.Text, new HashSet <Type> { typeof(string), typeof(char) } }, { TypeGroup.Number, new HashSet <Type> { typeof(int), typeof(uint), typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(long), typeof(ulong), typeof(Single), typeof(double), typeof(decimal) } }, { TypeGroup.Boolean, new HashSet <Type> { typeof(bool) } }, { TypeGroup.Date, new HashSet <Type> { typeof(DateTime) } }, { TypeGroup.Nullable, new HashSet <Type> { typeof(Nullable <>) } } }; Validate(); }
public static Filter <T> CreateFilters <T>(List <EntityFilter> valFilter) where T : class { var filter = new Filter <T>(); T valInstace = Activator.CreateInstance <T>(); PropertyInfo[] vProperties = valInstace.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (EntityFilter item in valFilter) { var vProperty = vProperties.FirstOrDefault(p => p.Name.Equals(item.PropertyName, StringComparison.CurrentCultureIgnoreCase)); eBooleanOperatorType vBooleanOperatorType = (eBooleanOperatorType)Enum.Parse(typeof(eBooleanOperatorType), item.BooleanOperator); eLogicOperatorType vLogicOperatorType = (eLogicOperatorType)Enum.Parse(typeof(eLogicOperatorType), item.LogicOperator); object vValue1 = item.Value1; object vValue2 = item.Value2; GetValue(vProperty, item, vBooleanOperatorType, ref vValue1, ref vValue2); filter.Add(item.PropertyName, vBooleanOperatorType, vLogicOperatorType, vValue1, vValue2); } return(filter); }
public void Add <TPropertyType>(string valPropertyName, eBooleanOperatorType valBooleanOperator, eLogicOperatorType valLogicOperator, TPropertyType value, TPropertyType value2) { IFilterStatement statement = new FilterStatement <TPropertyType>(valPropertyName, valBooleanOperator, value, value2, valLogicOperator); _Statements.Add(statement); }
public void Add <TPropertyType>(string valPropertyName, eBooleanOperatorType valBooleanOperator, eLogicOperatorType valLogicOperator, TPropertyType value) { Add <TPropertyType>(valPropertyName, valBooleanOperator, valLogicOperator, value, default(TPropertyType)); }