-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Option`1.cs
100 lines (81 loc) · 3.15 KB
/
Option`1.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis.Options
{
/// <summary>
/// Marker interface for <see cref="Option{T}"/>
/// </summary>
internal interface ILanguageSpecificOption : IOptionWithGroup
{
}
/// <summary>
/// An global option. An instance of this class can be used to access an option value from an OptionSet.
/// </summary>
public class Option<T> : ILanguageSpecificOption
{
/// <summary>
/// Feature this option is associated with.
/// </summary>
public string Feature { get; }
/// <summary>
/// Optional group/sub-feature for this option.
/// </summary>
internal OptionGroup Group { get; }
/// <summary>
/// The name of the option.
/// </summary>
public string Name { get; }
/// <summary>
/// The default value of the option.
/// </summary>
public T DefaultValue { get; }
/// <summary>
/// The type of the option value.
/// </summary>
public Type Type => typeof(T);
public ImmutableArray<OptionStorageLocation> StorageLocations { get; }
public Option(string feature, string name)
: this(feature, name, default)
{
// This constructor forwards to the next one; it exists to maintain source-level compatibility with older callers.
}
public Option(string feature, string name, T defaultValue)
: this(feature, name, defaultValue, storageLocations: Array.Empty<OptionStorageLocation>())
{
}
public Option(string feature, string name, T defaultValue, params OptionStorageLocation[] storageLocations)
: this(feature, group: OptionGroup.Default, name, defaultValue, storageLocations)
{
}
internal Option(string feature, OptionGroup group, string name, T defaultValue, params OptionStorageLocation[] storageLocations)
{
if (string.IsNullOrWhiteSpace(feature))
{
throw new ArgumentNullException(nameof(feature));
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(nameof(name));
}
this.Feature = feature;
this.Group = group ?? throw new ArgumentNullException(nameof(group));
this.Name = name;
this.DefaultValue = defaultValue;
this.StorageLocations = storageLocations.ToImmutableArray();
}
object IOption.DefaultValue => this.DefaultValue;
bool IOption.IsPerLanguage => false;
OptionGroup IOptionWithGroup.Group => this.Group;
public override string ToString()
{
return string.Format("{0} - {1}", this.Feature, this.Name);
}
public static implicit operator OptionKey(Option<T> option)
{
return new OptionKey(option);
}
}
}