瀏覽代碼

更新StaticField分析器 支持属性和字段 (#479)

susices 2 年之前
父節點
當前提交
6619a1c017

+ 4 - 5
Share/Analyzer/Analyzer/StaticFieldDeclarationAnalyzer.cs

@@ -37,15 +37,14 @@ namespace ET.Analyzer
 
             foreach (ISymbol? memberSymbol in namedTypeSymbol.GetMembers())
             {
-                // 筛选出非Const字段成员
-                if (memberSymbol is IFieldSymbol fieldSymbol && fieldSymbol.IsStatic&& !fieldSymbol.IsConst)
+                if (memberSymbol is IFieldSymbol { IsConst: false,IsStatic:true } or IPropertySymbol { IsStatic: true })
                 {
-                    bool hasAttr = fieldSymbol.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.StaticFieldAttribute);
+                    bool hasAttr = memberSymbol.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.StaticFieldAttribute);
                     if (!hasAttr)
                     {
-                        ReportDiagnostic(fieldSymbol);
+                        ReportDiagnostic(memberSymbol);
                     }
-                } 
+                }
             }
 
             void ReportDiagnostic(ISymbol symbol)

+ 1 - 1
Unity/Assets/Scripts/Core/Analyzer/StaticFieldAttribute.cs

@@ -7,7 +7,7 @@ namespace ET
     /// valueToAssign: 初始化时的字段值
     /// assignNewTypeInstance: 从默认构造函数初始化
     /// </summary>
-    [AttributeUsage(AttributeTargets.Field)]
+    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
     public class StaticFieldAttribute: Attribute
     {
         public readonly object valueToAssign;