-
Notifications
You must be signed in to change notification settings - Fork 219
/
BinaryExpressionHelper.cs
56 lines (46 loc) · 1.98 KB
/
BinaryExpressionHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.
using System.Activities.Runtime;
using System.Activities.Validation;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
namespace System.Activities.Expressions;
internal static class BinaryExpressionHelper
{
public static void OnGetArguments<TLeft, TRight>(CodeActivityMetadata metadata, InArgument<TLeft> left, InArgument<TRight> right)
{
RuntimeArgument rightArgument = new("Right", typeof(TRight), ArgumentDirection.In, true);
metadata.Bind(right, rightArgument);
RuntimeArgument leftArgument = new("Left", typeof(TLeft), ArgumentDirection.In, true);
metadata.Bind(left, leftArgument);
metadata.SetArgumentsCollection(
new Collection<RuntimeArgument>
{
rightArgument,
leftArgument
});
}
public static bool TryGenerateLinqDelegate<TLeft, TRight, TResult>(ExpressionType operatorType, out Func<TLeft, TRight, TResult> function, out ValidationError validationError)
{
function = null;
validationError = null;
ParameterExpression leftParameter = Expression.Parameter(typeof(TLeft), "left");
ParameterExpression rightParameter = Expression.Parameter(typeof(TRight), "right");
try
{
BinaryExpression binaryExpression = Expression.MakeBinary(operatorType, leftParameter, rightParameter);
Expression<Func<TLeft, TRight, TResult>> lambdaExpression = Expression.Lambda<Func<TLeft, TRight, TResult>>(binaryExpression, leftParameter, rightParameter);
function = lambdaExpression.Compile();
return true;
}
catch (Exception e)
{
if (Fx.IsFatal(e))
{
throw;
}
validationError = new ValidationError(e.Message);
return false;
}
}
}