| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System;
- using System.Collections.Immutable;
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- using Microsoft.CodeAnalysis.Diagnostics;
- namespace ET.Analyzer
- {
- [DiagnosticAnalyzer(LanguageNames.CSharp)]
- public class EntityDelegateDeclarationAnalyzer: DiagnosticAnalyzer
- {
- private const string Title = "实体类禁止声明委托字段或属性";
- private const string MessageFormat = "实体类: {0} 不能在类内部声明委托字段或属性: {1}";
- private const string Description = "实体类禁止声明委托字段或属性.";
-
- private const string EntityType = "ET.Entity";
-
- private static readonly DiagnosticDescriptor Rule =
- new DiagnosticDescriptor(DiagnosticIds.DelegateAnalyzerRuleId,
- Title,
- MessageFormat,
- DiagnosticCategories.Model,
- DiagnosticSeverity.Error,
- true,
- Description);
- public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>ImmutableArray.Create(Rule);
-
- public override void Initialize(AnalysisContext context)
- {
- if (!AnalyzerGlobalSetting.EnableAnalyzer)
- {
- return;
- }
- context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
- context.EnableConcurrentExecution();
- context.RegisterSymbolAction(this.Analyzer, SymbolKind.NamedType);
- }
- private void Analyzer(SymbolAnalysisContext context)
- {
- if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllModel))
- {
- return;
- }
-
- if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol))
- {
- return;
- }
- // 筛选出实体类
- if (namedTypeSymbol.BaseType?.ToString() != EntityType)
- {
- return;
- }
- foreach (var member in namedTypeSymbol.GetMembers())
- {
-
- if (member is IFieldSymbol fieldSymbol && fieldSymbol.Type.BaseType?.ToString()==typeof(MulticastDelegate).FullName)
- {
- ReportDiagnostic(fieldSymbol,fieldSymbol.Name);
- continue;
- }
-
- if (member is IPropertySymbol propertySymbol && propertySymbol.Type.BaseType?.ToString()==typeof(MulticastDelegate).FullName)
- {
- ReportDiagnostic(propertySymbol,propertySymbol.Name);
- continue;
- }
- }
-
- void ReportDiagnostic(ISymbol symbol,string delegateName)
- {
- foreach (var syntaxReference in symbol.DeclaringSyntaxReferences)
- {
- var syntax = syntaxReference.GetSyntax();
- Diagnostic diagnostic = Diagnostic.Create(Rule, syntax.GetLocation(),namedTypeSymbol.Name,delegateName);
- context.ReportDiagnostic(diagnostic);
- }
- }
- }
- }
- }
|