-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolReferencePropertyBinder.cs
More file actions
48 lines (39 loc) · 1.99 KB
/
Copy pathSymbolReferencePropertyBinder.cs
File metadata and controls
48 lines (39 loc) · 1.99 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
using System.CommandLine.Invocation;
using System.CommandLine.PropertyMapBinder.Internals;
using System.Linq.Expressions;
using System.Reflection;
namespace System.CommandLine.PropertyMapBinder.PropertyBinders
{
public class SymbolReferencePropertyBinder<TInputModel, TProperty> : IPropertyBinder<TInputModel>
{
private readonly Func<TInputModel, TProperty, TInputModel> _propertySetter;
private readonly Symbol symbolReference;
public SymbolReferencePropertyBinder(Argument<TProperty> argumentRef, Func<TInputModel, TProperty, TInputModel> setter)
{
symbolReference = argumentRef;
_propertySetter = setter;
}
public SymbolReferencePropertyBinder(Argument<TProperty> argumentRef, Expression<Func<TInputModel, TProperty>> selectorLambda)
{
symbolReference = argumentRef;
_propertySetter = ReflectionHelper.SelectorToSetter(selectorLambda);
}
public SymbolReferencePropertyBinder(Option<TProperty> optionRef, Func<TInputModel, TProperty, TInputModel> setter)
{
symbolReference = optionRef;
_propertySetter = setter;
}
public SymbolReferencePropertyBinder(Option<TProperty> optionRef, Expression<Func<TInputModel, TProperty>> selectorLambda)
{
symbolReference = optionRef;
_propertySetter = ReflectionHelper.SelectorToSetter(selectorLambda);
}
public TInputModel Bind(TInputModel inputModel, InvocationContext context)
{
var command = InvocationContextHelpers.GetCurrentCommand(context);
if (!InvocationContextHelpers.IsSymbolRegisteredOnCommand(command, symbolReference)) throw InvocationContextHelpers.MappedSymbolDoesntExist(command, symbolReference.Name);
TProperty propertyValue = InvocationContextHelpers.GetValueForSymbol<TProperty>(context, symbolReference);
return _propertySetter(inputModel, propertyValue);
}
}
}