本文整理汇总了C#中SymbolAnalysisContext.IsGenerated方法的典型用法代码示例。如果您正苦于以下问题:C# SymbolAnalysisContext.IsGenerated方法的具体用法?C# SymbolAnalysisContext.IsGenerated怎么用?C# SymbolAnalysisContext.IsGenerated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolAnalysisContext
的用法示例。
在下文中一共展示了SymbolAnalysisContext.IsGenerated方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Analyze
private static void Analyze(SymbolAnalysisContext context)
{
if (context.IsGenerated()) return;
var symbol = (INamedTypeSymbol)context.Symbol;
if (symbol.TypeKind != TypeKind.Class) return;
if (!symbol.Interfaces.Any(i => i.SpecialType == SpecialType.System_IDisposable)) return;
if (symbol.IsSealed && !ContainsUserDefinedFinalizer(symbol)) return;
if (!ContainsNonPrivateConstructors(symbol)) return;
var disposeMethod = FindDisposeMethod(symbol);
if (disposeMethod == null) return;
var syntaxTree = disposeMethod.DeclaringSyntaxReferences[0]?.GetSyntax();
var statements = ((MethodDeclarationSyntax)syntaxTree)?.Body?.Statements.OfType<ExpressionStatementSyntax>();
if (statements != null)
{
foreach (var statement in statements)
{
var invocation = statement.Expression as InvocationExpressionSyntax;
var method = invocation?.Expression as MemberAccessExpressionSyntax;
var identifierSyntax = method?.Expression as IdentifierNameSyntax;
if (identifierSyntax != null && identifierSyntax.Identifier.ToString() == "GC" && method.Name.ToString() == "SuppressFinalize")
return;
}
}
context.ReportDiagnostic(Diagnostic.Create(Rule, disposeMethod.Locations[0], symbol.Name));
}
示例2: Analyze
private static void Analyze(SymbolAnalysisContext context)
{
if (context.IsGenerated()) return;
var type = (INamedTypeSymbol)context.Symbol;
if (type.TypeKind != TypeKind.Class) return;
if (!IsAttribute(type)) return;
if (type.IsAbstract || type.IsSealed) return;
context.ReportDiagnostic(Diagnostic.Create(Rule, type.Locations[0], type.Name));
}
示例3: AnalyzeField
private static void AnalyzeField(SymbolAnalysisContext context)
{
if (context.IsGenerated()) return;
var fieldSymbol = (IFieldSymbol)context.Symbol;
if (!fieldSymbol.Type.AllInterfaces.Any(i => i.ToString() == "System.IDisposable") && fieldSymbol.Type.ToString() != "System.IDisposable") return;
var fieldSyntaxRef = fieldSymbol.DeclaringSyntaxReferences.FirstOrDefault();
var variableDeclarator = fieldSyntaxRef.GetSyntax() as VariableDeclaratorSyntax;
if (variableDeclarator == null) return;
if (ContainingTypeImplementsIDisposableAndCallsItOnTheField(context, fieldSymbol, fieldSymbol.ContainingType)) return;
var props = new Dictionary<string, string> { { "variableIdentifier", variableDeclarator.Identifier.ValueText } }.ToImmutableDictionary();
if (variableDeclarator.Initializer?.Value is InvocationExpressionSyntax)
context.ReportDiagnostic(Diagnostic.Create(RuleForReturned, variableDeclarator.GetLocation(), props, fieldSymbol.Name));
else if (variableDeclarator.Initializer?.Value is ObjectCreationExpressionSyntax)
context.ReportDiagnostic(Diagnostic.Create(RuleForCreated, variableDeclarator.GetLocation(), props, fieldSymbol.Name));
}