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