2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System ;
5
+ using System . Collections . Immutable ;
5
6
using System . Diagnostics ;
6
7
using System . Text . RegularExpressions ;
7
8
using Microsoft . CodeAnalysis ;
@@ -17,7 +18,6 @@ private sealed partial class Emitter
17
18
private readonly SourceGenerationSpec _sourceGenSpec ;
18
19
19
20
private bool _emitBlankLineBeforeNextStatement ;
20
- private bool _useFullyQualifiedNames ;
21
21
private int _valueSuffixIndex ;
22
22
23
23
private static readonly Regex s_arrayBracketsRegex = new ( Regex . Escape ( "[]" ) ) ;
@@ -32,7 +32,7 @@ public Emitter(SourceProductionContext context, SourceGenerationSpec sourceGenSp
32
32
33
33
public void Emit ( )
34
34
{
35
- if ( ! ShouldEmitBinders ( ) )
35
+ if ( ! ShouldEmitBindingExtensions ( ) )
36
36
{
37
37
return ;
38
38
}
@@ -42,17 +42,26 @@ public void Emit()
42
42
#nullable enable
43
43
#pragma warning disable CS0612, CS0618 // Suppress warnings about [Obsolete] member usage in generated code.
44
44
""" ) ;
45
- _writer . WriteLine ( ) ;
46
45
47
- _useFullyQualifiedNames = true ;
48
- EmitBinder_Extensions_IConfiguration ( ) ;
49
- EmitBinder_Extensions_OptionsBuilder ( ) ;
50
- EmitBinder_Extensions_IServiceCollection ( ) ;
46
+ EmitInterceptsLocationAttrDecl ( ) ;
47
+
48
+ EmitStartBlock ( $ "namespace { ProjectName } ") ;
49
+ EmitUsingStatements ( ) ;
50
+
51
+ _writer . WriteLine ( ) ;
52
+ EmitStartBlock ( $$ """
53
+ {{ Expression . GeneratedCodeAnnotation }}
54
+ file static class {{ Identifier . BindingExtensions }}
55
+ """ ) ;
56
+ EmitBindingExtensions_IConfiguration ( ) ;
57
+ EmitBindingExtensions_OptionsBuilder ( ) ;
58
+ EmitBindingExtensions_IServiceCollection ( ) ;
59
+ EmitCoreBindingHelpers ( ) ;
60
+ EmitEndBlock ( ) ; // BindingExtensions class
51
61
52
- _useFullyQualifiedNames = false ;
53
- Emit_CoreBindingHelper ( ) ;
62
+ EmitEndBlock ( ) ; // Binding namespace.
54
63
55
- _context . AddSource ( $ "{ Identifier . GeneratedConfigurationBinder } .g.cs", _writer . ToSourceText ( ) ) ;
64
+ _context . AddSource ( $ "{ Identifier . BindingExtensions } .g.cs", _writer . ToSourceText ( ) ) ;
56
65
}
57
66
58
67
private void EmitBindCoreCall (
@@ -74,7 +83,7 @@ private void EmitBindCoreCall(
74
83
if ( initKind is InitializationKind . AssignmentWithNullCheck )
75
84
{
76
85
Debug . Assert ( ! type . IsValueType ) ;
77
- _writer . WriteLine ( $ "{ type . MinimalDisplayString } ? { tempIdentifier } = { memberAccessExpr } ;") ;
86
+ _writer . WriteLine ( $ "{ type . DisplayString } ? { tempIdentifier } = { memberAccessExpr } ;") ;
78
87
EmitBindCoreCall ( tempIdentifier , InitializationKind . AssignmentWithNullCheck ) ;
79
88
}
80
89
else if ( initKind is InitializationKind . None && type . IsValueType )
@@ -89,9 +98,7 @@ private void EmitBindCoreCall(
89
98
90
99
void EmitBindCoreCall ( string objExpression , InitializationKind initKind )
91
100
{
92
- string methodDisplayString = GetHelperMethodDisplayString ( nameof ( MethodsToGen_CoreBindingHelper . BindCore ) ) ;
93
- string bindCoreCall = $@ "{ methodDisplayString } ({ configArgExpr } , ref { objExpression } , { Identifier . binderOptions } );";
94
-
101
+ string bindCoreCall = $@ "{ nameof ( MethodsToGen_CoreBindingHelper . BindCore ) } ({ configArgExpr } , ref { objExpression } , { Identifier . binderOptions } );";
95
102
EmitObjectInit ( objExpression , initKind ) ;
96
103
_writer . WriteLine ( bindCoreCall ) ;
97
104
writeOnSuccess ? . Invoke ( objExpression ) ;
@@ -127,12 +134,11 @@ private void EmitBindLogicFromString(
127
134
}
128
135
else if ( typeKind is StringParsableTypeKind . Enum )
129
136
{
130
- parsedValueExpr = $ "ParseEnum<{ type . MinimalDisplayString } >({ stringValueToParse_Expr } , () => { sectionPathExpr } )";
137
+ parsedValueExpr = $ "ParseEnum<{ type . DisplayString } >({ stringValueToParse_Expr } , () => { sectionPathExpr } )";
131
138
}
132
139
else
133
140
{
134
- string helperMethodDisplayString = GetHelperMethodDisplayString ( type . ParseMethodName ) ;
135
- parsedValueExpr = $ "{ helperMethodDisplayString } ({ stringValueToParse_Expr } , () => { sectionPathExpr } )";
141
+ parsedValueExpr = $ "{ type . ParseMethodName } ({ stringValueToParse_Expr } , () => { sectionPathExpr } )";
136
142
}
137
143
138
144
if ( ! checkForNullSectionValue )
@@ -156,7 +162,7 @@ private bool EmitObjectInit(TypeSpec type, string memberAccessExpr, Initializati
156
162
string initExpr ;
157
163
CollectionSpec ? collectionType = type as CollectionSpec ;
158
164
159
- string effectiveDisplayString = GetTypeDisplayString ( type ) ;
165
+ string effectiveDisplayString = type . DisplayString ;
160
166
if ( collectionType is not null )
161
167
{
162
168
if ( collectionType is EnumerableSpec { InitializationStrategy : InitializationStrategy . Array } )
@@ -165,7 +171,7 @@ private bool EmitObjectInit(TypeSpec type, string memberAccessExpr, Initializati
165
171
}
166
172
else
167
173
{
168
- effectiveDisplayString = GetTypeDisplayString ( collectionType . ConcreteType ?? collectionType ) ;
174
+ effectiveDisplayString = ( collectionType . ConcreteType ?? collectionType ) . DisplayString ;
169
175
initExpr = $ "new { effectiveDisplayString } ()";
170
176
}
171
177
}
@@ -215,36 +221,41 @@ private bool EmitObjectInit(TypeSpec type, string memberAccessExpr, Initializati
215
221
return true ;
216
222
}
217
223
218
- private void EmitCastToIConfigurationSection ( )
224
+ private void EmitInterceptsLocationAttrDecl ( )
219
225
{
220
- string sectionTypeDisplayString ;
221
- string exceptionTypeDisplayString ;
222
- if ( _useFullyQualifiedNames )
223
- {
224
- sectionTypeDisplayString = "global::Microsoft.Extensions.Configuration.IConfigurationSection" ;
225
- exceptionTypeDisplayString = FullyQualifiedDisplayString . InvalidOperationException ;
226
- }
227
- else
228
- {
229
- sectionTypeDisplayString = Identifier . IConfigurationSection ;
230
- exceptionTypeDisplayString = nameof ( InvalidOperationException ) ;
231
- }
232
-
226
+ _writer . WriteLine ( ) ;
233
227
_writer . WriteLine ( $$ """
234
- if ( {{ Identifier . configuration }} is not {{ sectionTypeDisplayString }} {{ Identifier . section }} )
228
+ namespace System.Runtime.CompilerServices
235
229
{
236
- throw new {{ exceptionTypeDisplayString }} ();
230
+ using System;
231
+ using System.CodeDom.Compiler;
232
+
233
+ {{ Expression . GeneratedCodeAnnotation }}
234
+ [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
235
+ file sealed class InterceptsLocationAttribute : Attribute
236
+ {
237
+ public InterceptsLocationAttribute(string filePath, int line, int column)
238
+ {
239
+ }
240
+ }
237
241
}
238
242
""" ) ;
243
+ _writer . WriteLine ( ) ;
244
+ }
245
+
246
+ private void EmitUsingStatements ( )
247
+ {
248
+ foreach ( string @namespace in _sourceGenSpec . Namespaces . ToImmutableSortedSet ( ) )
249
+ {
250
+ _writer . WriteLine ( $ "using { @namespace } ;") ;
251
+ }
239
252
}
240
253
241
254
private void EmitIConfigurationHasValueOrChildrenCheck ( bool voidReturn )
242
255
{
243
256
string returnPostfix = voidReturn ? string . Empty : " null" ;
244
- string methodDisplayString = GetHelperMethodDisplayString ( Identifier . HasValueOrChildren ) ;
245
-
246
257
_writer . WriteLine ( $$ """
247
- if (!{{ methodDisplayString }} ({{ Identifier . configuration }} ))
258
+ if (!{{ Identifier . HasValueOrChildren }} ({{ Identifier . configuration }} ))
248
259
{
249
260
return{{ returnPostfix }} ;
250
261
}
0 commit comments