Skip to content

Commit 0136af4

Browse files
Merge pull request #429 from Lempireqc/master
save
2 parents 8077310 + 37bd0e2 commit 0136af4

File tree

4 files changed

+163
-3
lines changed

4 files changed

+163
-3
lines changed

Z.Dynamic.Core.Lab/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Program
1111
static void Main(string[] args)
1212
{
1313

14-
Request_PropertyCaseSensitive.Execute();
14+
Request_LambdaAddFunc.Execute();
1515

1616
//var data = new List<object> {
1717
// new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Dynamic.Core;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
7+
namespace Z.Dynamic.Core.Lab
8+
{
9+
class Request_LambdaAddFunc
10+
{
11+
public static void Execute()
12+
{
13+
var expression = (Action<int>)DynamicExpressionParser.ParseLambda(typeof(Action<int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
14+
var expression2 = (Func<int, int>)DynamicExpressionParser.ParseLambda(typeof(Func<int, int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
15+
16+
expression(3);
17+
}
18+
}
19+
}

src/System.Linq.Dynamic.Core/DynamicExpressionParser.cs

+117-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
3030
return Expression.Lambda(parser.Parse(resultType, createParameterCtor));
3131
}
3232

33+
// NEED TEXT!
34+
[PublicAPI]
35+
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
36+
{
37+
Check.NotEmpty(expression, nameof(expression));
38+
39+
var parser = new ExpressionParser(new ParameterExpression[0], expression, values, parsingConfig);
40+
41+
return Expression.Lambda(delegateType, parser.Parse(resultType, createParameterCtor));
42+
}
43+
3344
/// <summary>
3445
/// Parses an expression into a Typed Expression.
3546
/// </summary>
@@ -45,6 +56,12 @@ public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Parsing
4556
return (Expression<Func<TResult>>)ParseLambda(parsingConfig, createParameterCtor, typeof(TResult), expression, values);
4657
}
4758

59+
[PublicAPI]
60+
public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] string expression, params object[] values)
61+
{
62+
return (Expression<Func<TResult>>)ParseLambda(delegateType, parsingConfig, createParameterCtor, typeof(TResult), expression, values);
63+
}
64+
4865
/// <summary>
4966
/// Parses an expression into a LambdaExpression.
5067
/// </summary>
@@ -57,7 +74,16 @@ public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Parsing
5774
/// <returns>The generated <see cref="LambdaExpression"/></returns>
5875
[PublicAPI]
5976
public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
77+
{
78+
return ParseLambda(parsingConfig, createParameterCtor, parameters, resultType, null, expression, values);
79+
}
80+
81+
// NEED TEXT!
82+
[PublicAPI]
83+
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
6084
{
85+
LambdaExpression lambdaExpression = null;
86+
6187
Check.NotNull(parameters, nameof(parameters));
6288
Check.HasNoNulls(parameters, nameof(parameters));
6389
Check.NotEmpty(expression, nameof(expression));
@@ -71,12 +97,28 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
7197
var renamer = new ParameterExpressionRenamer(parser.LastLambdaItName);
7298
parsedExpression = renamer.Rename(parsedExpression, out ParameterExpression newParameterExpression);
7399

74-
return Expression.Lambda(parsedExpression, new[] { newParameterExpression });
100+
if (delegateType == null)
101+
{
102+
lambdaExpression = Expression.Lambda(parsedExpression, new[] { newParameterExpression });
103+
}
104+
else
105+
{
106+
lambdaExpression = Expression.Lambda(delegateType, parsedExpression, new[] { newParameterExpression });
107+
}
75108
}
76109
else
77110
{
78-
return Expression.Lambda(parsedExpression, parameters);
111+
if (delegateType == null)
112+
{
113+
lambdaExpression = Expression.Lambda(parsedExpression, parameters);
114+
}
115+
else
116+
{
117+
lambdaExpression = Expression.Lambda(delegateType, parsedExpression, parameters);
118+
}
79119
}
120+
121+
return lambdaExpression;
80122
}
81123

82124
/// <summary>
@@ -95,6 +137,13 @@ public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Parsing
95137
return (Expression<Func<TResult>>)ParseLambda(parsingConfig, createParameterCtor, parameters, typeof(TResult), expression, values);
96138
}
97139

140+
// NEED TEXT!
141+
[PublicAPI]
142+
public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [NotNull] string expression, params object[] values)
143+
{
144+
return (Expression<Func<TResult>>)ParseLambda(delegateType, parsingConfig, createParameterCtor, parameters, typeof(TResult), expression, values);
145+
}
146+
98147
/// <summary>
99148
/// Parses an expression into a LambdaExpression.
100149
/// </summary>
@@ -131,6 +180,25 @@ public static Expression<Func<T, TResult>> ParseLambda<T, TResult>([CanBeNull] P
131180
return (Expression<Func<T, TResult>>)ParseLambda(parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(typeof(T), string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, typeof(TResult), expression, values);
132181
}
133182

183+
// NEED TEXT
184+
185+
// COMMENT ---- BEGIN ----
186+
// COMMENT ---- BEGIN ----
187+
// COMMENT ---- BEGIN ----
188+
189+
// Not sur for this method.
190+
191+
// COMMENT ---- END ----
192+
// COMMENT ---- END ----
193+
// COMMENT ---- END ----
194+
[PublicAPI]
195+
public static Expression<Func<T, TResult>> ParseLambda<T, TResult>([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] string expression, params object[] values)
196+
{
197+
Check.NotEmpty(expression, nameof(expression));
198+
199+
return (Expression<Func<T, TResult>>)ParseLambda(delegateType, parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(typeof(T), string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, typeof(TResult), expression, values);
200+
}
201+
134202
/// <summary>
135203
/// Parses an expression into a LambdaExpression. (Also create a constructor for all the parameters. Note that this doesn't work for Linq-to-Database entities.)
136204
/// </summary>
@@ -145,6 +213,13 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
145213
return ParseLambda(parsingConfig, true, resultType, expression, values);
146214
}
147215

216+
// NEED TEXT
217+
[PublicAPI]
218+
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
219+
{
220+
return ParseLambda(delegateType, parsingConfig, true, resultType, expression, values);
221+
}
222+
148223
/// <summary>
149224
/// Parses an expression into a LambdaExpression. (Also create a constructor for all the parameters. Note that this doesn't work for Linq-to-Database entities.)
150225
/// </summary>
@@ -160,6 +235,15 @@ public static LambdaExpression ParseLambda([CanBeNull] Type resultType, [NotNull
160235
return ParseLambda(null, true, resultType, expression, values);
161236
}
162237

238+
//// NEED TEXT
239+
//[PublicAPI]
240+
//public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
241+
//{
242+
// Check.NotEmpty(expression, nameof(expression));
243+
244+
// return ParseLambda(delegateType, null, true, resultType, expression, values);
245+
//}
246+
163247
/// <summary>
164248
/// Parses an expression into a LambdaExpression. (Also create a constructor for all the parameters. Note that this doesn't work for Linq-to-Database entities.)
165249
/// </summary>
@@ -189,6 +273,13 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
189273
return ParseLambda(parsingConfig, true, itType, resultType, expression, values);
190274
}
191275

276+
// NEED TEXT!
277+
[PublicAPI]
278+
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, [NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
279+
{
280+
return ParseLambda(delegateType, parsingConfig, true, itType, resultType, expression, values);
281+
}
282+
192283
/// <summary>
193284
/// Parses an expression into a LambdaExpression.
194285
/// </summary>
@@ -208,6 +299,16 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
208299
return ParseLambda(parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(itType, string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, resultType, expression, values);
209300
}
210301

302+
// NEED TEXT!
303+
[PublicAPI]
304+
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
305+
{
306+
Check.NotNull(itType, nameof(itType));
307+
Check.NotEmpty(expression, nameof(expression));
308+
309+
return ParseLambda(parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(itType, string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, resultType, expression, values);
310+
}
311+
211312
/// <summary>
212313
/// Parses an expression into a LambdaExpression. (Also create a constructor for all the parameters. Note that this doesn't work for Linq-to-Database entities.)
213314
/// </summary>
@@ -222,6 +323,13 @@ public static LambdaExpression ParseLambda([NotNull] ParameterExpression[] param
222323
return ParseLambda(null, true, parameters, resultType, expression, values);
223324
}
224325

326+
// NEED TEXT!
327+
[PublicAPI]
328+
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, string expression, params object[] values)
329+
{
330+
return ParseLambda(delegateType, null, true, parameters, resultType, expression, values);
331+
}
332+
225333
/// <summary>
226334
/// Parses an expression into a LambdaExpression. (Also create a constructor for all the parameters. Note that this doesn't work for Linq-to-Database entities.)
227335
/// </summary>
@@ -237,6 +345,13 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
237345
return ParseLambda(parsingConfig, true, parameters, resultType, expression, values);
238346
}
239347

348+
// NEED TEXT!
349+
[PublicAPI]
350+
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, string expression, params object[] values)
351+
{
352+
return ParseLambda(delegateType, parsingConfig, true, parameters, resultType, expression, values);
353+
}
354+
240355
/// <summary>
241356
/// Parses an expression into a LambdaExpression.
242357
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using NFluent;
9+
10+
namespace System.Linq.Dynamic.Core.Tests.MikArea
11+
{
12+
public class ParseLambda
13+
{
14+
[Fact]
15+
public void Test_ParseLambda_1()
16+
{
17+
18+
var expression = (Action<int>)DynamicExpressionParser.ParseLambda(typeof(Action<int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
19+
var expression2 = (Func<int, int>)DynamicExpressionParser.ParseLambda(typeof(Func<int, int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
20+
21+
expression(3);
22+
var t = expression2(4);
23+
Check.That(t).IsEqualTo(5);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)