-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathFeatureSwitches.cs
More file actions
253 lines (217 loc) · 11.7 KB
/
FeatureSwitches.cs
File metadata and controls
253 lines (217 loc) · 11.7 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.CompilerServices;
namespace WinRT
{
/// <summary>
/// A container for all shared <see cref="AppContext"/> configuration switches for CsWinRT.
/// </summary>
/// <remarks>
/// <para>
/// This type uses a very specific setup for configuration switches to ensure ILLink can work the best.
/// This mirrors the architecture of feature switches in the runtime as well, and it's needed so that
/// no static constructor is generated for the type.
/// </para>
/// <para>
/// For more info, see <see href="https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md#adding-new-feature-switch"/>.
/// </para>
/// </remarks>
internal static class FeatureSwitches
{
/// <summary>
/// The configuration property name for <see cref="EnableDynamicObjectsSupport"/>.
/// </summary>
private const string EnableDynamicObjectsSupportPropertyName = "CSWINRT_ENABLE_DYNAMIC_OBJECTS_SUPPORT";
/// <summary>
/// The configuration property name for <see cref="UseExceptionResourceKeys"/>.
/// </summary>
private const string UseExceptionResourceKeysPropertyName = "CSWINRT_USE_EXCEPTION_RESOURCE_KEYS";
/// <summary>
/// The configuration property name for <see cref="EnableDefaultCustomTypeMappings"/>.
/// </summary>
private const string EnableDefaultCustomTypeMappingsPropertyName = "CSWINRT_ENABLE_DEFAULT_CUSTOM_TYPE_MAPPINGS";
/// <summary>
/// The configuration property name for <see cref="EnableICustomPropertyProviderSupport"/>.
/// </summary>
private const string EnableICustomPropertyProviderSupportPropertyName = "CSWINRT_ENABLE_ICUSTOMPROPERTYPROVIDER_SUPPORT";
/// <summary>
/// The configuration property name for <see cref="EnableIReferenceSupport"/>.
/// </summary>
private const string EnableIReferenceSupportPropertyName = "CSWINRT_ENABLE_IREFERENCE_SUPPORT";
/// <summary>
/// The configuration property name for <see cref="EnableIDynamicInterfaceCastableSupport"/>.
/// </summary>
private const string EnableIDynamicInterfaceCastableSupportPropertyName = "CSWINRT_ENABLE_IDYNAMICINTERFACECASTABLE";
/// <summary>
/// The configuration property name for <see cref="EnableManifestFreeActivation"/>.
/// </summary>
private const string EnableManifestFreeActivationPropertyName = "CSWINRT_ENABLE_MANIFEST_FREE_ACTIVATION";
/// <summary>
/// The configuration property name for <see cref="ManifestFreeActivationReportOriginalException"/>.
/// </summary>
private const string ManifestFreeActivationReportOriginalExceptionPropertyName = "CSWINRT_MANIFEST_FREE_ACTIVATION_REPORT_ORIGINAL_EXCEPTION";
/// <summary>
/// The configuration property name for <see cref="UseWindowsUIXamlProjections"/>.
/// </summary>
private const string UseWindowsUIXamlProjectionsPropertyName = "CSWINRT_USE_WINDOWS_UI_XAML_PROJECTIONS";
/// <summary>
/// The configuration property name for <see cref="SuppressCustomPropertyNotSupportedException"/>.
/// </summary>
private const string SuppressCustomPropertyNotSupportedExceptionPropertyName = "CSWINRT_SUPPRESS_CUSTOM_PROPERTY_NOT_SUPPORTED_EXCEPTION";
/// <summary>
/// The backing field for <see cref="EnableDynamicObjectsSupport"/>.
/// </summary>
private static int _enableDynamicObjectsSupport;
/// <summary>
/// The backing field for <see cref="UseExceptionResourceKeys"/>.
/// </summary>
private static int _useExceptionResourceKeys;
/// <summary>
/// The backing field for <see cref="EnableDefaultCustomTypeMappings"/>.
/// </summary>
private static int _enableDefaultCustomTypeMappings;
/// <summary>
/// The backing field for <see cref="EnableICustomPropertyProviderSupport"/>.
/// </summary>
private static int _enableICustomPropertyProviderSupport;
/// <summary>
/// The backing field for <see cref="EnableIReferenceSupport"/>.
/// </summary>
private static int _enableIReferenceSupport;
/// <summary>
/// The backing field for <see cref="EnableIDynamicInterfaceCastableSupport"/>.
/// </summary>
private static int _enableIDynamicInterfaceCastableSupport;
/// <summary>
/// The backing field for <see cref="EnableManifestFreeActivation"/>.
/// </summary>
private static int _enableManifestFreeActivation;
/// <summary>
/// The backing field for <see cref="ManifestFreeActivationReportOriginalException"/>.
/// </summary>
private static int _manifestFreeActivationReportOriginalException;
/// <summary>
/// The backing field for <see cref="UseWindowsUIXamlProjections"/>.
/// </summary>
private static int _useWindowsUIXamlProjections;
/// <summary>
/// The backing field for <see cref="SuppressCustomPropertyNotSupportedException"/>.
/// </summary>
private static int _suppressCustomPropertyNotSupportedException;
/// <summary>
/// Gets a value indicating whether or not projections support for dynamic objects is enabled (defaults to <see langword="true"/>).
/// </summary>
public static bool EnableDynamicObjectsSupport
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(EnableDynamicObjectsSupportPropertyName, ref _enableDynamicObjectsSupport, true);
}
/// <summary>
/// Gets a value indicating whether or not exceptions should use resource keys rather than localized messages (defaults to <see langword="false"/>).
/// </summary>
public static bool UseExceptionResourceKeys
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(UseExceptionResourceKeysPropertyName, ref _useExceptionResourceKeys, false);
}
/// <summary>
/// Gets a value indicating whether or not <see cref="Projections"/> should initialize all default type mappings automatically (defaults to <see langword="true"/>).
/// </summary>
public static bool EnableDefaultCustomTypeMappings
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(EnableDefaultCustomTypeMappingsPropertyName, ref _enableDefaultCustomTypeMappings, true);
}
/// <summary>
/// Gets a value indicating whether or not <see cref="ABI.Microsoft.UI.Xaml.Data.ManagedCustomPropertyProviderVftbl"/> should be enabled (defaults to <see langword="true"/>).
/// </summary>
public static bool EnableICustomPropertyProviderSupport
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(EnableICustomPropertyProviderSupportPropertyName, ref _enableICustomPropertyProviderSupport, true);
}
/// <summary>
/// Gets a value indicating whether or not <c>IReference<T></c>, <c>IReferenceArray<T></c> and <c>IPropertyValue</c> CCW implementations should be supported (defaults to <see langword="true"/>).
/// </summary>
public static bool EnableIReferenceSupport
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(EnableIReferenceSupportPropertyName, ref _enableIReferenceSupport, true);
}
/// <summary>
/// Gets a value indicating whether or not <see cref="System.Runtime.InteropServices.IDynamicInterfaceCastable"/> should be supported by RCW types (defaults to <see langword="true"/>).
/// </summary>
public static bool EnableIDynamicInterfaceCastableSupport
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(EnableIDynamicInterfaceCastableSupportPropertyName, ref _enableIDynamicInterfaceCastableSupport, true);
}
/// <summary>
/// Gets a value indicating whether or not manifest free WinRT activation is supported (defaults to <see langword="true"/>).
/// </summary>
public static bool EnableManifestFreeActivation
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(EnableManifestFreeActivationPropertyName, ref _enableManifestFreeActivation, true);
}
/// <summary>
/// Gets a value indicating whether or not the original exception should be thrown if activation fails when <see cref="EnableManifestFreeActivation"/> is disabled (defaults to <see langword="false"/>).
/// </summary>
public static bool ManifestFreeActivationReportOriginalException
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(ManifestFreeActivationReportOriginalExceptionPropertyName, ref _manifestFreeActivationReportOriginalException, false);
}
/// <summary>
/// Gets a value indicating whether to project .NET types to their <c>Windows.UI.Xaml</c> equivalents instead of their <c>Microsoft.UI.Xaml</c> equivalents.
/// </summary>
public static bool UseWindowsUIXamlProjections
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(UseWindowsUIXamlProjectionsPropertyName, ref _useWindowsUIXamlProjections, false);
}
/// <summary>
/// Gets a value indicating whether to suppress the <see cref="NotSupportedException"/> thrown when a type does not implement
/// <c>IBindableCustomPropertyImplementation</c> on AOT, and return <see langword="null"/> instead (defaults to <see langword="false"/>).
/// </summary>
public static bool SuppressCustomPropertyNotSupportedException
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetConfigurationValue(SuppressCustomPropertyNotSupportedExceptionPropertyName, ref _suppressCustomPropertyNotSupportedException, false);
}
/// <summary>
/// Gets a configuration value for a specified property.
/// </summary>
/// <param name="propertyName">The property name to retrieve the value for.</param>
/// <param name="cachedResult">The cached result for the target configuration value.</param>
/// <returns>The value of the specified configuration setting.</returns>
private static bool GetConfigurationValue(string propertyName, ref int cachedResult, bool defaultValue)
{
// The cached switch value has 3 states:
// 0: unknown.
// 1: true
// -1: false
//
// This method doesn't need to worry about concurrent accesses to the cached result,
// as even if the configuration value is retrieved twice, that'll always be the same.
if (cachedResult < 0)
{
return false;
}
if (cachedResult > 0)
{
return true;
}
// Get the configuration switch value, or its default.
// All feature switches have a default set in the .targets file.
if (!AppContext.TryGetSwitch(propertyName, out bool isEnabled))
{
isEnabled = defaultValue;
}
// Update the cached result
cachedResult = isEnabled ? 1 : -1;
return isEnabled;
}
}
}