-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTypeParameterBuilder.cs
More file actions
189 lines (166 loc) · 7.24 KB
/
TypeParameterBuilder.cs
File metadata and controls
189 lines (166 loc) · 7.24 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Linq;
using Optional;
using Optional.Unsafe;
namespace SharpCode;
/// <summary>
/// Provides functionalty for building type parameters.
/// <see cref="TypeParameterBuilder"/> instances are <b>not</b> immutable.
/// </summary>
public class TypeParameterBuilder
{
internal TypeParameterBuilder()
{
}
internal TypeParameter TypeParameter { get; private set; } = new TypeParameter(name: Option.None<string>());
/// <summary>
/// Sets the name of the type parameter.
/// </summary>
/// <param name="name">The name of the type parameter.</param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="name"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// The specified <paramref name="name"/> is empty or invalid.
/// </exception>
public TypeParameterBuilder WithName(string name)
{
if (name is null)
throw new ArgumentNullException(nameof(name));
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("The type parameter name must be a valid, non-empty string.", nameof(name));
TypeParameter = TypeParameter.With(name: Option.Some(name));
return this;
}
/// <summary>
/// Adds a constraint to the the type parameter. See <see cref="TypeParameterConstraint"/>
/// for various constraints and their explanations.
/// </summary>
/// <param name="constraint">
/// The constraint to be added to the type parameter.
/// Optionally, you can use <see cref="TypeParameterConstraint"/> members.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specific <paramref name="constraint"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// The specific <paramref name="constraint"/> is empty or invalid.
/// </exception>
public TypeParameterBuilder WithConstraint(string constraint)
{
if (constraint is null)
throw new ArgumentNullException(nameof(constraint));
if (string.IsNullOrWhiteSpace(constraint))
throw new ArgumentException("The type parameter constraint must be a valid, non-empty string.", nameof(constraint));
TypeParameter.Constraints.Add(constraint);
return this;
}
/// <summary>
/// Adds a bunch of constraints to the type parameter. See <see cref="TypeParameterConstraint"/>
/// for various constraints and their explanations.
/// </summary>
/// <param name="constraints">
/// The constraints to be added to the type parameter.
/// Optionally, you can use <see cref="TypeParameterConstraint"/> members.
/// </param>
/// <exception cref="ArgumentException">
/// One of the specified <paramref name="constraints"/> is <c>null</c> or
/// an empty (invalid) string.
/// </exception>
public TypeParameterBuilder WithConstraints(params string[] constraints)
{
if (constraints.Any(string.IsNullOrWhiteSpace))
throw new ArgumentException("One of the constraints value is null or empty.");
TypeParameter.Constraints.AddRange(constraints);
return this;
}
/// <summary>
/// Adds a bunch of constraints to the type parameter. See <see cref="TypeParameterConstraint"/>
/// for various constraints and their explanations.
/// </summary>
/// <param name="constraints">
/// The constraints to be added to the type parameter.
/// Optionally, you can use <see cref="TypeParameterConstraint"/> members.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="constraints"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// One of the specified <paramref name="constraints"/> is <c>null</c> or
/// an empty (invalid) string.
/// </exception>
public TypeParameterBuilder WithConstraints(IEnumerable<string> constraints)
{
if (constraints is null)
throw new ArgumentNullException(nameof(constraints));
if (constraints.Any(string.IsNullOrWhiteSpace))
throw new ArgumentException("One of the constraints value is null or empty.");
TypeParameter.Constraints.AddRange(constraints);
return this;
}
internal TypeParameter Build()
{
if (string.IsNullOrWhiteSpace(TypeParameter.Name.ValueOrDefault()))
{
throw new MissingBuilderSettingException(
"Providing the name of the type parameter is required when building a type parameter.");
}
return TypeParameter;
}
}
/// <summary>
/// For more information about type paramater constraints, see the official documentation
/// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters .
/// </summary>
public static class TypeParameterConstraint
{
/// <summary>
/// The type argument must be a non-nullable value type.
/// For information about nullable value types, see Nullable value types.
/// Because all value types have an accessible parameterless constructor,
/// the struct constraint implies the new() constraint and can't be combined
/// with the new() constraint. You can't combine the struct constraint with
/// the unmanaged constraint.
/// </summary>
public const string Struct = "struct";
/// <summary>
/// The type argument must be a reference type. This constraint applies also
/// to any class, interface, delegate, or array type. In a nullable context
/// in C# 8.0 or later, T must be a non-nullable reference type.
/// </summary>
public const string Class = "class";
/// <summary>
/// The type argument must be a reference type, either nullable or
/// non-nullable. This constraint applies also to any class, interface,
/// delegate, or array type.
/// </summary>
public const string NullableClass = "class?";
/// <summary>
/// The type argument must be a non-nullable type. The argument can be a
/// non-nullable reference type in C# 8.0 or later, or a non-nullable value
/// type.
/// </summary>
public const string NotNull = "notnull";
/// <summary>
/// This constraint resolves the ambiguity when you need to specify an
/// unconstrained type parameter when you override a method or provide an
/// explicit interface implementation. The default constraint implies the
/// base method without either the class or struct constraint.
/// For more information, see the default constraint spec proposal.
/// </summary>
public const string Default = "default";
/// <summary>
/// The type argument must be a non-nullable unmanaged type. The unmanaged
/// constraint implies the struct constraint and can't be combined with
/// either the struct or new() constraints.
/// </summary>
public const string Unmanaged = "unmanaged";
/// <summary>
/// The type argument must have a public parameterless constructor. When
/// used together with other constraints, the new() constraint must be
/// specified last. The new() constraint can't be combined with the struct
/// and unmanaged constraints.
/// </summary>
public const string New = "new()";
}