Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
Lempireqc committed Oct 13, 2020
1 parent 8077310 commit 37bd0e2
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Z.Dynamic.Core.Lab/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Program
static void Main(string[] args)
{

Request_PropertyCaseSensitive.Execute();
Request_LambdaAddFunc.Execute();

//var data = new List<object> {
// new { ItemCode = "AAAA", Flag = true, SoNo="aaa",JobNo="JNO01" } ,
Expand Down
19 changes: 19 additions & 0 deletions Z.Dynamic.Core.Lab/Request_LambdaAddFunc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text;

namespace Z.Dynamic.Core.Lab
{
class Request_LambdaAddFunc
{
public static void Execute()
{
var expression = (Action<int>)DynamicExpressionParser.ParseLambda(typeof(Action<int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
var expression2 = (Func<int, int>)DynamicExpressionParser.ParseLambda(typeof(Func<int, int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();

expression(3);
}
}
}
119 changes: 117 additions & 2 deletions src/System.Linq.Dynamic.Core/DynamicExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
return Expression.Lambda(parser.Parse(resultType, createParameterCtor));
}

// NEED TEXT!
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
Check.NotEmpty(expression, nameof(expression));

var parser = new ExpressionParser(new ParameterExpression[0], expression, values, parsingConfig);

return Expression.Lambda(delegateType, parser.Parse(resultType, createParameterCtor));
}

/// <summary>
/// Parses an expression into a Typed Expression.
/// </summary>
Expand All @@ -45,6 +56,12 @@ public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Parsing
return (Expression<Func<TResult>>)ParseLambda(parsingConfig, createParameterCtor, typeof(TResult), expression, values);
}

[PublicAPI]
public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] string expression, params object[] values)
{
return (Expression<Func<TResult>>)ParseLambda(delegateType, parsingConfig, createParameterCtor, typeof(TResult), expression, values);
}

/// <summary>
/// Parses an expression into a LambdaExpression.
/// </summary>
Expand All @@ -57,7 +74,16 @@ public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Parsing
/// <returns>The generated <see cref="LambdaExpression"/></returns>
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
return ParseLambda(parsingConfig, createParameterCtor, parameters, resultType, null, expression, values);
}

// NEED TEXT!
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
LambdaExpression lambdaExpression = null;

Check.NotNull(parameters, nameof(parameters));
Check.HasNoNulls(parameters, nameof(parameters));
Check.NotEmpty(expression, nameof(expression));
Expand All @@ -71,12 +97,28 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
var renamer = new ParameterExpressionRenamer(parser.LastLambdaItName);
parsedExpression = renamer.Rename(parsedExpression, out ParameterExpression newParameterExpression);

return Expression.Lambda(parsedExpression, new[] { newParameterExpression });
if (delegateType == null)
{
lambdaExpression = Expression.Lambda(parsedExpression, new[] { newParameterExpression });
}
else
{
lambdaExpression = Expression.Lambda(delegateType, parsedExpression, new[] { newParameterExpression });
}
}
else
{
return Expression.Lambda(parsedExpression, parameters);
if (delegateType == null)
{
lambdaExpression = Expression.Lambda(parsedExpression, parameters);
}
else
{
lambdaExpression = Expression.Lambda(delegateType, parsedExpression, parameters);
}
}

return lambdaExpression;
}

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

// NEED TEXT!
[PublicAPI]
public static Expression<Func<TResult>> ParseLambda<TResult>([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] ParameterExpression[] parameters, [NotNull] string expression, params object[] values)
{
return (Expression<Func<TResult>>)ParseLambda(delegateType, parsingConfig, createParameterCtor, parameters, typeof(TResult), expression, values);
}

/// <summary>
/// Parses an expression into a LambdaExpression.
/// </summary>
Expand Down Expand Up @@ -131,6 +180,25 @@ public static LambdaExpression ParseLambda(bool createParameterCtor, [NotNull] T
return (Expression<Func<T, TResult>>)ParseLambda(parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(typeof(T), string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, typeof(TResult), expression, values);
}

// NEED TEXT

// COMMENT ---- BEGIN ----
// COMMENT ---- BEGIN ----
// COMMENT ---- BEGIN ----

// Not sur for this method.

// COMMENT ---- END ----
// COMMENT ---- END ----
// COMMENT ---- END ----
[PublicAPI]
public static Expression<Func<T, TResult>> ParseLambda<T, TResult>([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] string expression, params object[] values)
{
Check.NotEmpty(expression, nameof(expression));

return (Expression<Func<T, TResult>>)ParseLambda(delegateType, parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(typeof(T), string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, typeof(TResult), expression, values);
}

/// <summary>
/// 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.)
/// </summary>
Expand All @@ -145,6 +213,13 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
return ParseLambda(parsingConfig, true, resultType, expression, values);
}

// NEED TEXT
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
{
return ParseLambda(delegateType, parsingConfig, true, resultType, expression, values);
}

/// <summary>
/// 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.)
/// </summary>
Expand All @@ -160,6 +235,15 @@ public static LambdaExpression ParseLambda([CanBeNull] Type resultType, [NotNull
return ParseLambda(null, true, resultType, expression, values);
}

//// NEED TEXT
//[PublicAPI]
//public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] Type resultType, [NotNull] string expression, params object[] values)
//{
// Check.NotEmpty(expression, nameof(expression));

// return ParseLambda(delegateType, null, true, resultType, expression, values);
//}

/// <summary>
/// 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.)
/// </summary>
Expand Down Expand Up @@ -189,6 +273,13 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
return ParseLambda(parsingConfig, true, itType, resultType, expression, values);
}

// NEED TEXT!
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, [NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
{
return ParseLambda(delegateType, parsingConfig, true, itType, resultType, expression, values);
}

/// <summary>
/// Parses an expression into a LambdaExpression.
/// </summary>
Expand All @@ -208,6 +299,16 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
return ParseLambda(parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(itType, string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, resultType, expression, values);
}

// NEED TEXT!
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, bool createParameterCtor, [NotNull] Type itType, [CanBeNull] Type resultType, string expression, params object[] values)
{
Check.NotNull(itType, nameof(itType));
Check.NotEmpty(expression, nameof(expression));

return ParseLambda(parsingConfig, createParameterCtor, new[] { ParameterExpressionHelper.CreateParameterExpression(itType, string.Empty, parsingConfig?.RenameEmptyParameterExpressionNames ?? false) }, resultType, expression, values);
}

/// <summary>
/// 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.)
/// </summary>
Expand All @@ -222,6 +323,13 @@ public static LambdaExpression ParseLambda([NotNull] ParameterExpression[] param
return ParseLambda(null, true, parameters, resultType, expression, values);
}

// NEED TEXT!
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, string expression, params object[] values)
{
return ParseLambda(delegateType, null, true, parameters, resultType, expression, values);
}

/// <summary>
/// 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.)
/// </summary>
Expand All @@ -237,6 +345,13 @@ public static LambdaExpression ParseLambda([CanBeNull] ParsingConfig parsingConf
return ParseLambda(parsingConfig, true, parameters, resultType, expression, values);
}

// NEED TEXT!
[PublicAPI]
public static LambdaExpression ParseLambda([CanBeNull] Type delegateType, [CanBeNull] ParsingConfig parsingConfig, [NotNull] ParameterExpression[] parameters, [CanBeNull] Type resultType, string expression, params object[] values)
{
return ParseLambda(delegateType, parsingConfig, true, parameters, resultType, expression, values);
}

/// <summary>
/// Parses an expression into a LambdaExpression.
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/MikArea/ParseLambda.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using NFluent;

namespace System.Linq.Dynamic.Core.Tests.MikArea
{
public class ParseLambda
{
[Fact]
public void Test_ParseLambda_1()
{

var expression = (Action<int>)DynamicExpressionParser.ParseLambda(typeof(Action<int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();
var expression2 = (Func<int, int>)DynamicExpressionParser.ParseLambda(typeof(Func<int, int>), new[] { Expression.Parameter(typeof(int), "x") }, typeof(int), "x + 1").Compile();

expression(3);
var t = expression2(4);
Check.That(t).IsEqualTo(5);
}
}
}

0 comments on commit 37bd0e2

Please sign in to comment.