From a329bee8e8f4098abb2981e2a434b1fcb4aead0d Mon Sep 17 00:00:00 2001 From: "boxu.zheng" Date: Tue, 18 Jul 2023 14:29:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E7=9A=84=20guid=20=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppBase/Filters/FilterExtensions.cs | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) 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