-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathParameterDescriptor.cs
66 lines (54 loc) · 2 KB
/
ParameterDescriptor.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
57
58
59
60
61
62
63
64
65
66
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CommandLine.Binding;
using System.Reflection;
namespace System.CommandLine.NamingConventionBinder;
/// <summary>
/// Provides information for binding command line input to a method or constructor parameter.
/// </summary>
public class ParameterDescriptor : IValueDescriptor
{
private readonly ParameterInfo _parameterInfo;
private bool? _allowsNull;
internal ParameterDescriptor(
ParameterInfo parameterInfo,
IMethodDescriptor parent)
{
Parent = parent;
_parameterInfo = parameterInfo;
}
/// <inheritdoc />
public string ValueName => _parameterInfo.Name;
/// <summary>
/// The method descriptor that this constructor belongs to.
/// </summary>
public IMethodDescriptor Parent { get; }
/// <inheritdoc />
public Type ValueType => _parameterInfo.ParameterType;
/// <inheritdoc />
public bool HasDefaultValue => _parameterInfo.HasDefaultValue;
/// <summary>
/// Gets a value indicating whether <see langword="null"/> is allowed to be passed for the target parameter.
/// </summary>
public bool AllowsNull
{
get
{
if (_allowsNull is null)
{
_allowsNull = CalculateAllowsNull(_parameterInfo);
}
return _allowsNull ?? false;
}
}
internal static bool CalculateAllowsNull(ParameterInfo parameterInfo)
=> parameterInfo.ParameterType.IsNullable() ||
parameterInfo.HasDefaultValue && parameterInfo.DefaultValue is null;
/// <inheritdoc />
public object? GetDefaultValue() =>
_parameterInfo.DefaultValue is DBNull
? ArgumentConverter.GetDefaultValue(ValueType)
: _parameterInfo.DefaultValue;
/// <inheritdoc />
public override string ToString() => $"{ValueType.Name} {ValueName}";
}