本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax.Ancestors方法的典型用法代码示例。如果您正苦于以下问题:C# MethodDeclarationSyntax.Ancestors方法的具体用法?C# MethodDeclarationSyntax.Ancestors怎么用?C# MethodDeclarationSyntax.Ancestors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax
的用法示例。
在下文中一共展示了MethodDeclarationSyntax.Ancestors方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MissingAnalysisMethodAsync
// adds an analysis method to the enclosing class
private async Task<Document> MissingAnalysisMethodAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string analysisMethodName = CodeFixHelper.AnalysisMethodName(declaration);
SemanticModel semanticModel = await document.GetSemanticModelAsync();
var newAnalysisMethod = CodeFixHelper.CreateAnalysisMethod(generator, analysisMethodName, semanticModel) as MethodDeclarationSyntax;
ClassDeclarationSyntax classDeclaration = declaration.Ancestors().OfType<ClassDeclarationSyntax>().First();
return await ReplaceNode(classDeclaration, classDeclaration.AddMembers(newAnalysisMethod), document);
}
示例2: MethodFullName
/// <summary>
/// returns fullname of a methoddeclaration
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
private string MethodFullName(MethodDeclarationSyntax m)
{
//may be a simplest way exists, but it works
string result = m.Identifier.Text;
var ancestors = m.Ancestors();
SyntaxNode classOrStruct;
var classe = ancestors.OfType<ClassDeclarationSyntax>().FirstOrDefault();
if (classe != null)
{
result = classe.Identifier.Text + "." + result;
classOrStruct = classe;
}
else
{
var structe = ancestors.OfType<StructDeclarationSyntax>().FirstOrDefault();
if (structe == null)
{
//it's just a peace of code with out class or struct
return result;
}
result = structe.Identifier.Text + "." + result;
classOrStruct = structe;
}
var ns = classOrStruct.Ancestors().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
var nsString = ns?.Name.ToString();
result = nsString + "." + result;
return result;
}
示例3: CreateMethodSyncVariant
/// <summary>
/// Creates a sync method variant for the given async method.
/// </summary>
/// <param name="document">The code analysis document.</param>
/// <param name="methodDecl">The async method.</param>
/// <param name="semanticModel">A semantic model to look for the type in.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The modified document.</returns>
private async Task<Document> CreateMethodSyncVariant(Document document, MethodDeclarationSyntax methodDecl, SemanticModel semanticModel, CancellationToken cancellationToken)
{
// Setup
MethodDeclarationSyntax syncMethodDecl;
// Are we dealing with interface or class?
var typeDecl = methodDecl.Ancestors().OfType<TypeDeclarationSyntax>().FirstOrDefault();
if (typeDecl is ClassDeclarationSyntax)
{
// Create the async method signature
syncMethodDecl = await CodeGeneration.ImplementSyncMethodVariant(document, methodDecl, semanticModel, cancellationToken);
}
else
{
// Create the async method signature
syncMethodDecl = await CodeGeneration.ImplementSyncMethodVariantSignature(document, methodDecl, semanticModel, cancellationToken);
}
// Update the solution
var root = await document.GetSyntaxRootAsync(cancellationToken);
// Insert
document = document.WithSyntaxRoot(root.InsertNodesBefore(methodDecl, new[] { syncMethodDecl }));
// Format
document = await Formatter.FormatAsync(document, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
document = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
return document;
}