本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.Traverse方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.Traverse方法的具体用法?C# SyntaxNode.Traverse怎么用?C# SyntaxNode.Traverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.Traverse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateTypesAsync
private async Task<string> GenerateTypesAsync(Document document, SyntaxNode node, CancellationToken cancellationToken)
{
// Track the types that have already been exported
var exportedNodes = new List<INamedTypeSymbol>();
var nodes = node.Traverse().Where(n =>
{
return n is TypeDeclarationSyntax;
//|| n is InterfaceDeclarationSyntax;
});
//var interfaces = nodes.OfType<InterfaceDeclarationSyntax>().ToArray();
var types = nodes.OfType<TypeDeclarationSyntax>().ToArray();
var sb = new System.Text.StringBuilder();
var comp = await document.Project.GetCompilationAsync();
foreach (var typeDec in types)
{
var NamespaceDec = typeDec.BottomUpFirstOfType<NamespaceDeclarationSyntax>();
sb.AppendLine($"declare module {NamespaceDec.GetFullName()} {{");
var si = comp.GetTypeByMetadataName(NamespaceDec.GetFullName() + "." + typeDec.Identifier.Text);
exportedNodes.Add(si);
exportedNodes.Add(si.BaseType);
exportedNodes.AddRange(si.Interfaces);
sb.Append($"\texport class {typeDec.Identifier.Text}");
if (si.BaseType != null)
{
sb.Append(" extends ");
if (!si.BaseType.CanBeReferencedByName)
sb.Append(si.BaseType.ContainingNamespace + ".");
sb.Append(si.BaseType.Name);
sb.AppendLine(" {{");
}
foreach (var member in typeDec.Members)
{
if (member is FieldDeclarationSyntax)
{
var field = member as FieldDeclarationSyntax;
var fieldIdentifier = field.Declaration.Variables.FirstOrDefault().Identifier;
//fieldIdentifier.Value
string type = GetTypeScriptEquivalentType(field.Declaration.Type);
sb.AppendLine($"\t\t{fieldIdentifier.Text}: {type};");
}
else if (member is PropertyDeclarationSyntax)
{
var property = member as PropertyDeclarationSyntax;
//var fieldIdentifier = property..FirstOrDefault().Identifier;
//fieldIdentifier.Value
var accessor = property.Modifiers.Any(z => string.Equals(z.Text, "public")) ? "public" : "private";
string type = GetTypeScriptEquivalentType(property.Type);
sb.AppendLine($"\t\t{accessor} {property.Identifier.Text}: {type};");
}
}
sb.AppendLine("\t}");
sb.AppendLine("}");
}
var grouped = exportedNodes.GroupBy(x => x.ContainingNamespace);
return sb.ToString();
}