diff --git a/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/Filters/FilterExtensions.cs b/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/Filters/FilterExtensions.cs index 7f75390..bcd3e4c 100644 --- a/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/Filters/FilterExtensions.cs +++ b/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/Filters/FilterExtensions.cs @@ -71,10 +71,11 @@ public static class FilterExtensions { var parameter = Expression.Parameter(typeof(T), "p"); //创建参数p var member = Expression.PropertyOrField(parameter, filter.Column); //创建表达式中的属性或字段 - // var propertyType = member.Type; //取属性类型,常量constant按此类型进行转换 - //var constant = Expression.Constant(filterCondition.Value);//创建常数 ConstantExpression constant = null; + //var propertyType = member.Type; //取属性类型,常量constant按此类型进行转换 + //constant = Expression.Constant(filterCondition.Value);//创建常数 + if (filter.Action != "In" && filter.Action != "NotIn") { constant = CreateConstantExpression(member.Type, filter.Value); @@ -140,14 +141,17 @@ public static class FilterExtensions /// private static ConstantExpression CreateConstantExpression(Type propertyType, string value) { - ConstantExpression constant; + ConstantExpression constant = null; try { if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) { - var objValue = Convert.ChangeType(value, propertyType.GetGenericArguments()[0], + + var objValue = Convert.ChangeType(ChangeTypeReturnValue(value, propertyType.GetGenericArguments()[0]), + propertyType.GetGenericArguments()[0], CultureInfo.InvariantCulture); + constant = Expression.Constant(objValue, propertyType); } else if (propertyType.IsEnum) @@ -333,4 +337,34 @@ public static class FilterExtensions var inv = Expression.Invoke(condition, exp.Parameters); return Expression.Lambda>(Expression.And(exp.Body, inv), exp.Parameters); } + + /// + /// 转换传入的值,并将正确的值传出(解决 enum,guid) + /// + /// + /// + /// + public static object ChangeTypeReturnValue(object value, Type type) + { + if (value == null && type.IsGenericType) return Activator.CreateInstance(type); + if (value == null) return null; + if (type == value.GetType()) return value; + if (type.IsEnum) + { + if (value is string) + return Enum.Parse(type, value as string); + else + return Enum.ToObject(type, value); + } + if (!type.IsInterface && type.IsGenericType) + { + Type innerType = type.GetGenericArguments()[0]; + object innerValue = ChangeTypeReturnValue(value, innerType); + return Activator.CreateInstance(type, new object[] { innerValue }); + } + if (value is string && type == typeof(Guid)) return new Guid(value as string); + if (value is string && type == typeof(Version)) return new Version(value as string); + if (!(value is IConvertible)) return value; + return Convert.ChangeType(value, type); + } } \ No newline at end of file