forked from ful-stackz/SharpCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldBuilder.cs
More file actions
124 lines (110 loc) · 3.52 KB
/
FieldBuilder.cs
File metadata and controls
124 lines (110 loc) · 3.52 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
using System;
using Optional;
namespace SharpCode
{
/// <summary>
/// Provides functionality for building fields. <see cref="FieldBuilder"/> instances are <b>not</b> immutable.
/// </summary>
public class FieldBuilder
{
private readonly Field _field = new Field();
internal FieldBuilder()
{
}
internal FieldBuilder(AccessModifier accessModifier, string type, string name)
{
_field.AccessModifier = accessModifier;
_field.Type = type;
_field.Name = name;
}
internal FieldBuilder(AccessModifier accessModifier, Type type, string name)
: this(accessModifier, type.Name, name)
{
}
/// <summary>
/// Sets accessibilty modifier of the field being built.
/// </summary>
public FieldBuilder WithAccessModifier(AccessModifier accessModifier)
{
_field.AccessModifier = accessModifier;
return this;
}
/// <summary>
/// Sets the type of the field being built.
/// </summary>
public FieldBuilder WithType(string type)
{
_field.Type = type;
return this;
}
/// <summary>
/// Sets the type of the field being built.
/// </summary>
public FieldBuilder WithType(Type type)
{
_field.Type = type.Name;
return this;
}
/// <summary>
/// Sets the name of the field being built.
/// </summary>
public FieldBuilder WithName(string name)
{
_field.Name = name;
return this;
}
/// <summary>
/// Sets the readonly preference for the field being built.
/// </summary>
/// <param name="makeReadonly">
/// Indicates whether the field will be made readonly (<c>true</c>) or not (<c>false</c>).
/// </param>
public FieldBuilder MakeReadonly(bool makeReadonly = true)
{
_field.IsReadonly = makeReadonly;
return this;
}
/// <summary>
/// Adds XML summary documentation to the field.
/// </summary>
/// <param name="summary">
/// The content of the summary documentation.
/// </param>
public FieldBuilder WithSummary(string summary)
{
_field.Summary = Option.Some<string?>(summary);
return this;
}
/// <summary>
/// Returns the source code of the built field.
/// </summary>
/// <param name="formatted">
/// Indicates whether to format the source code.
/// </param>
public string ToSourceCode(bool formatted = true)
{
return Build().ToSourceCode(formatted);
}
/// <summary>
/// Returns the source code of the built field.
/// </summary>
public override string ToString()
{
return ToSourceCode();
}
internal Field Build()
{
if (string.IsNullOrWhiteSpace(_field.Type))
{
throw new MissingBuilderSettingException(
"Providing the type of the field is required when building a field.");
}
else if (string.IsNullOrWhiteSpace(_field.Name))
{
throw new MissingBuilderSettingException(
"Providing the name of the field is required when building a field.");
}
return _field;
}
}
}