Skip to content

Commit 53b388c

Browse files
committed
重载ToLambda扩展方法;优化ToObject扩展方法;
1 parent 0e7a988 commit 53b388c

1 file changed

Lines changed: 24 additions & 65 deletions

File tree

ZqUtils.Core/Extensions/Extensions.Linq.cs

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,29 @@ public static Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> Wh
10831083
#endregion
10841084

10851085
#region ToLambda
1086+
/// <summary>
1087+
/// ToLambda
1088+
/// </summary>
1089+
/// <param name="this"></param>
1090+
/// <param name="parameters"></param>
1091+
/// <returns></returns>
1092+
public static LambdaExpression ToLambda(this Expression @this, params ParameterExpression[] parameters)
1093+
{
1094+
return Expression.Lambda(@this, parameters);
1095+
}
1096+
1097+
/// <summary>
1098+
/// ToLambda
1099+
/// </summary>
1100+
/// <param name="this"></param>
1101+
/// <param name="delegateType"></param>
1102+
/// <param name="parameters"></param>
1103+
/// <returns></returns>
1104+
public static LambdaExpression ToLambda(this Expression @this, Type delegateType, params ParameterExpression[] parameters)
1105+
{
1106+
return Expression.Lambda(delegateType, @this, parameters);
1107+
}
1108+
10861109
/// <summary>
10871110
/// ToLambda
10881111
/// </summary>
@@ -1104,7 +1127,6 @@ public static Expression<T> ToLambda<T>(this Expression @this, params ParameterE
11041127
/// <returns></returns>
11051128
public static object ToObject(this Expression @this)
11061129
{
1107-
var type = @this.Type;
11081130
var nodeType = @this.NodeType;
11091131

11101132
if (ExpressionType.Constant == nodeType)
@@ -1131,70 +1153,7 @@ public static object ToObject(this Expression @this)
11311153
}
11321154
}
11331155

1134-
if (typeof(string) == type)
1135-
return @this.ToLambda<Func<string>>().Compile()();
1136-
1137-
if (typeof(short) == type)
1138-
return @this.ToLambda<Func<short>>().Compile()();
1139-
1140-
if (typeof(short?) == type)
1141-
return @this.ToLambda<Func<short?>>().Compile()();
1142-
1143-
if (typeof(int) == type)
1144-
return @this.ToLambda<Func<int>>().Compile()();
1145-
1146-
if (typeof(int?) == type)
1147-
return @this.ToLambda<Func<int?>>().Compile()();
1148-
1149-
if (typeof(long) == type)
1150-
return @this.ToLambda<Func<long>>().Compile()();
1151-
1152-
if (typeof(long?) == type)
1153-
return @this.ToLambda<Func<long?>>().Compile()();
1154-
1155-
if (typeof(decimal) == type)
1156-
return @this.ToLambda<Func<decimal>>().Compile()();
1157-
1158-
if (typeof(decimal?) == type)
1159-
return @this.ToLambda<Func<decimal?>>().Compile()();
1160-
1161-
if (typeof(double) == type)
1162-
return @this.ToLambda<Func<double>>().Compile()();
1163-
1164-
if (typeof(double?) == type)
1165-
return @this.ToLambda<Func<double?>>().Compile()();
1166-
1167-
if (typeof(float) == type)
1168-
return @this.ToLambda<Func<float>>().Compile()();
1169-
1170-
if (typeof(float?) == type)
1171-
return @this.ToLambda<Func<float?>>().Compile()();
1172-
1173-
if (typeof(DateTime) == type)
1174-
return @this.ToLambda<Func<DateTime>>().Compile()();
1175-
1176-
if (typeof(DateTime?) == type)
1177-
return @this.ToLambda<Func<DateTime?>>().Compile()();
1178-
1179-
if (typeof(bool) == type)
1180-
return @this.ToLambda<Func<bool>>().Compile()();
1181-
1182-
if (typeof(bool?) == type)
1183-
return @this.ToLambda<Func<bool?>>().Compile()();
1184-
1185-
if (typeof(byte) == type)
1186-
return @this.ToLambda<Func<byte>>().Compile()();
1187-
1188-
if (typeof(byte?) == type)
1189-
return @this.ToLambda<Func<byte?>>().Compile()();
1190-
1191-
if (typeof(char) == type)
1192-
return @this.ToLambda<Func<char>>().Compile()();
1193-
1194-
if (typeof(char?) == type)
1195-
return @this.ToLambda<Func<char?>>().Compile()();
1196-
1197-
return @this.ToLambda<Func<object>>().Compile()();
1156+
return @this.ToLambda().Compile().DynamicInvoke();
11981157
}
11991158

12001159
/// <summary>

0 commit comments

Comments
 (0)