private static void AnalyzeFieldDeclaration(SyntaxNodeAnalysisContext context) { var fieldNode = (FieldDeclarationSyntax)context.Node; if (!fieldNode.ContainsDiagnostics) { foreach (var variable in fieldNode.Declaration.Variables) { var fieldSymbol = context.SemanticModel.GetDeclaredSymbol(variable) as IFieldSymbol; var classSymbol = fieldSymbol?.ContainingType; if (context.CancellationToken.IsCancellationRequested) { break; } if (fieldSymbol != null && classSymbol != null && classSymbol.IsStereotype()) { if (fieldSymbol.Type.IsIPropertyInfo()) { foreach (var classMember in classSymbol.GetMembers()) { if (classMember.Kind == SymbolKind.Property) { var classProperty = classMember as IPropertySymbol; if (!classProperty.IsIndexer) { if (EvaluateManagedBackingFieldsAnalayzer.DetermineIfPropertyUsesField( context, fieldSymbol, classProperty)) { if (context.CancellationToken.IsCancellationRequested) { break; } EvaluateManagedBackingFieldsAnalayzer.CheckForDiagnostics( context, fieldNode, fieldSymbol); break; } } } } } } } } }
private static bool DetermineIfPropertyUsesField(SyntaxNodeAnalysisContext context, IFieldSymbol fieldSymbol, IPropertySymbol classProperty) { if (classProperty.GetMethod != null) { return(EvaluateManagedBackingFieldsAnalayzer.DetermineIfPropertyUsesField( context, fieldSymbol, classProperty, propertyNode => propertyNode.ExpressionBody as SyntaxNode ?? propertyNode.AccessorList.Accessors.Single( _ => _.IsKind(SyntaxKind.GetAccessorDeclaration)).Body)); } if (classProperty.SetMethod != null) { return(EvaluateManagedBackingFieldsAnalayzer.DetermineIfPropertyUsesField( context, fieldSymbol, classProperty, propertyNode => propertyNode.AccessorList.Accessors.Single( _ => _.IsKind(SyntaxKind.SetAccessorDeclaration)).Body)); } return(false); }