-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathBinaryExpressionHelper.cs
More file actions
77 lines (67 loc) · 3.1 KB
/
BinaryExpressionHelper.cs
File metadata and controls
77 lines (67 loc) · 3.1 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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;
/// <summary>
/// Helpers for binary expressions.
/// </summary>
public static class BinaryExpressionHelper
{
/// <summary>
/// Binds metadata when getting arguments.
/// </summary>
/// <typeparam name="TLeft">The type of the left argument.</typeparam>
/// <typeparam name="TRight">The type of the right argument.</typeparam>
/// <param name="metadata">The metadata.</param>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
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
});
}
/// <summary>
/// Generates a <see cref="System.Linq"/> delegate.
/// </summary>
/// <typeparam name="TLeft">The type of the left argument.</typeparam>
/// <typeparam name="TRight">The type of the right argument.</typeparam>
/// <typeparam name="TResult">The return type of the operation.</typeparam>
/// <param name="operatorType">The type of expression.</param>
/// <param name="function">The resulting <see cref="Func{T1, T2, TResult}"/>.</param>
/// <param name="validationError">If the operation failed, the error.</param>
/// <returns><see langword="true"/> if the operation was successful; otherwise, <see langword="false"/>.</returns>
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;
}
}
}