-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathMemberMapParameter.cs
More file actions
62 lines (53 loc) · 2.13 KB
/
Copy pathMemberMapParameter.cs
File metadata and controls
62 lines (53 loc) · 2.13 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
using System;
using System.Linq;
using System.Linq.Expressions;
using WorkflowCore.Interface;
namespace WorkflowCore.Models
{
public class MemberMapParameter : IStepParameter
{
private readonly LambdaExpression _source;
private readonly LambdaExpression _target;
private readonly Delegate _compiledSource;
public MemberMapParameter(LambdaExpression source, LambdaExpression target)
{
if (target.Body.NodeType != ExpressionType.MemberAccess)
throw new NotSupportedException();
_source = source;
_target = target;
_compiledSource = source.Compile();
}
private void Assign(object sourceObject, object targetObject, IStepExecutionContext context)
{
object resolvedValue = null;
switch (_source.Parameters.Count)
{
case 1:
resolvedValue = _compiledSource.DynamicInvoke(sourceObject);
break;
case 2:
resolvedValue = _compiledSource.DynamicInvoke(sourceObject, context);
break;
default:
throw new ArgumentException();
}
if (resolvedValue == null)
{
var defaultAssign = Expression.Lambda(Expression.Assign(_target.Body, Expression.Default(_target.ReturnType)), _target.Parameters.Single());
defaultAssign.Compile().DynamicInvoke(targetObject);
return;
}
var valueExpr = Expression.Convert(Expression.Constant(resolvedValue), _target.ReturnType);
var assign = Expression.Lambda(Expression.Assign(_target.Body, valueExpr), _target.Parameters.Single());
assign.Compile().DynamicInvoke(targetObject);
}
public void AssignInput(object data, IStepBody body, IStepExecutionContext context)
{
Assign(data, body, context);
}
public void AssignOutput(object data, IStepBody body, IStepExecutionContext context)
{
Assign(body, data, context);
}
}
}