1717using System ;
1818using System . Collections . Generic ;
1919using System . Collections . Immutable ;
20+ using System . Diagnostics ;
2021using System . IO ;
2122using System . Linq ;
2223using System . Reflection ;
@@ -130,8 +131,10 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
130131 var classSymbol = semanticModel . GetDeclaredSymbol ( classDeclarationSyntax ) ;
131132
132133 if ( classSymbol is null )
134+ {
133135 return null ;
134-
136+ }
137+
135138 var attributes = new List < RegionInjectData > ( ) ;
136139
137140 foreach ( var attributeData in classSymbol . GetAttributes ( ) )
@@ -157,33 +160,43 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
157160 var placeholders = new List < string > ( ) ;
158161
159162 // 从构造函数参数获取占位符
160- if ( attributeData . ConstructorArguments . Length > 2 )
163+ // 检查是否使用了带 params string[] 的构造函数
164+ if ( attributeData . ConstructorArguments . Length >= 3 )
161165 {
162- for ( var i = 2 ; i < attributeData . ConstructorArguments . Length ; i ++ )
166+ // 第3个参数是 params string[] placeholders
167+ var placeholdersArg = attributeData . ConstructorArguments [ 2 ] ;
168+ if ( placeholdersArg . Kind == TypedConstantKind . Array && ! placeholdersArg . IsNull )
163169 {
164- var value = attributeData . ConstructorArguments [ i ] . Value ? . ToString ( ) ;
165- if ( ! string . IsNullOrEmpty ( value ) )
170+ var arrayValues = placeholdersArg . Values ;
171+ foreach ( var item in arrayValues )
166172 {
167- placeholders . Add ( value ) ;
173+ var stringValue = item . Value ? . ToString ( ) ;
174+ if ( ! string . IsNullOrEmpty ( stringValue ) )
175+ {
176+ placeholders . Add ( stringValue ) ;
177+ }
168178 }
169179 }
170180 }
171181
172- // 从 Placeholders 属性获取占位符
173- foreach ( var namedArgument in attributeData . NamedArguments )
182+ // 如果构造函数中没有占位符,再从 Placeholders 属性获取
183+ if ( placeholders . Count == 0 )
174184 {
175- if ( namedArgument . Key == "Placeholders" )
185+ foreach ( var namedArgument in attributeData . NamedArguments )
176186 {
177- var value = namedArgument . Value ;
178- if ( value . Kind == TypedConstantKind . Array && ! value . IsNull )
187+ if ( namedArgument . Key == "Placeholders" )
179188 {
180- var arrayValues = value . Values ;
181- foreach ( var item in arrayValues )
189+ var value = namedArgument . Value ;
190+ if ( value . Kind == TypedConstantKind . Array && ! value . IsNull )
182191 {
183- var stringValue = item . Value ? . ToString ( ) ;
184- if ( ! string . IsNullOrEmpty ( stringValue ) )
192+ var arrayValues = value . Values ;
193+ foreach ( var item in arrayValues )
185194 {
186- placeholders . Add ( stringValue ) ;
195+ var stringValue = item . Value ? . ToString ( ) ;
196+ if ( ! string . IsNullOrEmpty ( stringValue ) )
197+ {
198+ placeholders . Add ( stringValue ) ;
199+ }
187200 }
188201 }
189202 }
@@ -217,19 +230,25 @@ private static string GetNamespace(ClassDeclarationSyntax classDeclaration)
217230 {
218231 var namespaceDeclaration = classDeclaration . Ancestors ( ) . OfType < NamespaceDeclarationSyntax > ( ) . FirstOrDefault ( ) ;
219232 if ( namespaceDeclaration != null )
233+ {
220234 return namespaceDeclaration . Name . ToString ( ) ;
235+ }
221236
222237 var fileScopedNamespace = classDeclaration . Ancestors ( ) . OfType < FileScopedNamespaceDeclarationSyntax > ( ) . FirstOrDefault ( ) ;
223238 if ( fileScopedNamespace != null )
239+ {
224240 return fileScopedNamespace . Name . ToString ( ) ;
241+ }
225242
226243 return string . Empty ;
227244 }
228245
229246 private static void Execute ( ImmutableArray < ClassToGenerate > classes , ImmutableArray < AdditionalText > additionalFiles , SourceProductionContext context )
230247 {
231248 if ( classes . IsDefaultOrEmpty )
249+ {
232250 return ;
251+ }
233252
234253 foreach ( var classToGenerate in classes )
235254 {
@@ -285,7 +304,9 @@ private static string GenerateClass(ClassToGenerate classInfo, ImmutableArray<Ad
285304 private static string FormatInjectedCode ( string code , string indentation )
286305 {
287306 if ( string . IsNullOrEmpty ( code ) )
307+ {
288308 return string . Empty ;
309+ }
289310
290311 var lines = code . Split ( new [ ] { '\r ' , '\n ' } , StringSplitOptions . None ) ;
291312 var formattedLines = new List < string > ( ) ;
@@ -453,7 +474,9 @@ private static string ExtractRegion(string fileContent, string regionName)
453474 private static string ProcessPlaceholders ( string content , string [ ] placeholders )
454475 {
455476 if ( placeholders . Length == 0 )
477+ {
456478 return content ;
479+ }
457480
458481 var result = content ;
459482
0 commit comments