@@ -151,9 +151,39 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
151151
152152 string filePath = null ;
153153 string regionName = null ;
154+ var placeholders = new List < string > ( ) ;
155+
156+ // 首先从命名参数(属性)获取值
157+ foreach ( var namedArgument in attributeData . NamedArguments )
158+ {
159+ switch ( namedArgument . Key )
160+ {
161+ case "FilePath" :
162+ filePath = namedArgument . Value . Value ? . ToString ( ) ;
163+ break ;
164+ case "RegionName" :
165+ regionName = namedArgument . Value . Value ? . ToString ( ) ;
166+ break ;
167+ case "Placeholders" :
168+ var value = namedArgument . Value ;
169+ if ( value . Kind == TypedConstantKind . Array && ! value . IsNull )
170+ {
171+ var arrayValues = value . Values ;
172+ foreach ( var item in arrayValues )
173+ {
174+ var stringValue = item . Value ? . ToString ( ) ;
175+ if ( ! string . IsNullOrEmpty ( stringValue ) )
176+ {
177+ placeholders . Add ( stringValue ) ;
178+ }
179+ }
180+ }
181+ break ;
182+ }
183+ }
154184
155- // 处理不同的构造函数重载
156- if ( attributeData . ConstructorArguments . Length >= 1 )
185+ // 如果没有从属性获取到值,尝试从构造函数参数获取(向后兼容)
186+ if ( string . IsNullOrEmpty ( regionName ) && attributeData . ConstructorArguments . Length >= 1 )
157187 {
158188 var firstArg = attributeData . ConstructorArguments [ 0 ] . Value ? . ToString ( ) ;
159189
@@ -169,62 +199,34 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
169199 filePath = firstArg ;
170200 regionName = attributeData . ConstructorArguments [ 1 ] . Value ? . ToString ( ) ;
171201 }
172- }
173202
174- if ( string . IsNullOrEmpty ( regionName ) )
175- {
176- continue ;
177- }
178-
179- var placeholders = new List < string > ( ) ;
180-
181- // 从构造函数参数获取占位符
182- if ( attributeData . ConstructorArguments . Length >= 3 )
183- {
184- // 第3个参数可能是 params string[] placeholders
185- var placeholdersArg = attributeData . ConstructorArguments [ 2 ] ;
186- if ( placeholdersArg . Kind == TypedConstantKind . Array && ! placeholdersArg . IsNull )
187- {
188- var arrayValues = placeholdersArg . Values ;
189- foreach ( var item in arrayValues )
190- {
191- var stringValue = item . Value ? . ToString ( ) ;
192- if ( ! string . IsNullOrEmpty ( stringValue ) )
193- {
194- placeholders . Add ( stringValue ) ;
195- }
196- }
197- }
198- }
199- else if ( attributeData . ConstructorArguments . Length == 2 && string . IsNullOrEmpty ( filePath ) )
200- {
201- // 可能是 RegionInjectAttribute(regionName, params string[] placeholders)
202- var secondArg = attributeData . ConstructorArguments [ 1 ] ;
203- if ( secondArg . Kind == TypedConstantKind . Array && ! secondArg . IsNull )
203+ // 从构造函数参数获取占位符(如果属性中没有设置)
204+ if ( placeholders . Count == 0 )
204205 {
205- var arrayValues = secondArg . Values ;
206- foreach ( var item in arrayValues )
206+ if ( attributeData . ConstructorArguments . Length >= 3 )
207207 {
208- var stringValue = item . Value ? . ToString ( ) ;
209- if ( ! string . IsNullOrEmpty ( stringValue ) )
208+ // 第3个参数可能是 params string[] placeholders
209+ var placeholdersArg = attributeData . ConstructorArguments [ 2 ] ;
210+ if ( placeholdersArg . Kind == TypedConstantKind . Array && ! placeholdersArg . IsNull )
210211 {
211- placeholders . Add ( stringValue ) ;
212+ var arrayValues = placeholdersArg . Values ;
213+ foreach ( var item in arrayValues )
214+ {
215+ var stringValue = item . Value ? . ToString ( ) ;
216+ if ( ! string . IsNullOrEmpty ( stringValue ) )
217+ {
218+ placeholders . Add ( stringValue ) ;
219+ }
220+ }
212221 }
213222 }
214- }
215- }
216-
217- // 如果构造函数中没有占位符,再从 Placeholders 属性获取
218- if ( placeholders . Count == 0 )
219- {
220- foreach ( var namedArgument in attributeData . NamedArguments )
221- {
222- if ( namedArgument . Key == "Placeholders" )
223+ else if ( attributeData . ConstructorArguments . Length == 2 && string . IsNullOrEmpty ( filePath ) )
223224 {
224- var value = namedArgument . Value ;
225- if ( value . Kind == TypedConstantKind . Array && ! value . IsNull )
225+ // 可能是 RegionInjectAttribute(regionName, params string[] placeholders)
226+ var secondArg = attributeData . ConstructorArguments [ 1 ] ;
227+ if ( secondArg . Kind == TypedConstantKind . Array && ! secondArg . IsNull )
226228 {
227- var arrayValues = value . Values ;
229+ var arrayValues = secondArg . Values ;
228230 foreach ( var item in arrayValues )
229231 {
230232 var stringValue = item . Value ? . ToString ( ) ;
@@ -238,6 +240,11 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
238240 }
239241 }
240242
243+ if ( string . IsNullOrEmpty ( regionName ) )
244+ {
245+ continue ;
246+ }
247+
241248 // 获取属性的位置信息
242249 var attributeLocation = attributeData . ApplicationSyntaxReference ? . GetSyntax ( ) . GetLocation ( ) ?? Location . None ;
243250
@@ -701,32 +708,12 @@ namespace CodeInject
701708 [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
702709 public class RegionInjectAttribute : Attribute
703710 {
704- public string FilePath { get; }
705- public string RegionName { get; }
711+ public string FilePath { get; set; }
712+ public string RegionName { get; set; }
706713 public string[] Placeholders { get; set; } = new string[0];
707714
708- public RegionInjectAttribute(string regionName)
709- {
710- FilePath = null;
711- RegionName = regionName ?? throw new ArgumentNullException(nameof(regionName));
712- }
713-
714- public RegionInjectAttribute(string regionName, params string[] placeholders)
715- : this(regionName)
716- {
717- Placeholders = placeholders ?? new string[0];
718- }
719-
720- public RegionInjectAttribute(string filePath, string regionName)
721- {
722- FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
723- RegionName = regionName ?? throw new ArgumentNullException(nameof(regionName));
724- }
725-
726- public RegionInjectAttribute(string filePath, string regionName, params string[] placeholders)
727- : this(filePath, regionName)
715+ public RegionInjectAttribute()
728716 {
729- Placeholders = placeholders ?? new string[0];
730717 }
731718 }
732719}" ;
0 commit comments