本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetTypeByMetadataName方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpCompilation.GetTypeByMetadataName方法的具体用法?C# CSharpCompilation.GetTypeByMetadataName怎么用?C# CSharpCompilation.GetTypeByMetadataName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.CSharpCompilation
的用法示例。
在下文中一共展示了CSharpCompilation.GetTypeByMetadataName方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Rewrite
public IEnumerable<SyntaxTree> Rewrite(SyntaxTree[] syntaxTrees, CSharpCompilation compilationNode, string[] excludeTypes = null)
{
this.methodAttributesSymbol = compilationNode.GetTypeByMetadataName(typeof(MethodAttributes).FullName);
this.cancellationTokenSymbol = compilationNode.GetTypeByMetadataName(typeof(CancellationToken).FullName);
this.excludedTypes = new HashSet<ITypeSymbol>();
if (excludeTypes != null)
{
var excludedTypeSymbols = excludeTypes.Select(compilationNode.GetTypeByMetadataName).ToList();
var notFound = excludedTypeSymbols.IndexOf(null);
if (notFound != -1)
{
throw new ArgumentException($"Type {excludeTypes[notFound]} not found in compilation", nameof(excludeTypes));
}
this.excludedTypes.UnionWith(excludedTypeSymbols);
}
this.excludedTypes.UnionWith(alwaysExcludedTypeNames.Select(compilationNode.GetTypeByMetadataName).Where(sym => sym != null));
foreach (var syntaxTree in syntaxTrees)
{
var semanticModel = compilationNode.GetSemanticModel(syntaxTree, true);
if (semanticModel == null)
{
throw new ArgumentException("A provided syntax tree was compiled into the provided compilation");
}
var asyncMethods = syntaxTree
.GetRoot()
.DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.Where(m => m.Modifiers.Any(c => c.Kind() == SyntaxKind.AsyncKeyword));
foreach (var asyncMethod in asyncMethods)
{
ValidateAsyncMethod(asyncMethod, semanticModel);
}
if (syntaxTree.GetRoot().DescendantNodes().All(m => ((m as MethodDeclarationSyntax)?.AttributeLists ?? (m as TypeDeclarationSyntax)?.AttributeLists)?.SelectMany(al => al.Attributes).Any(a => a.Name.ToString().StartsWith("RewriteAsync")) == null))
{
continue;
}
var namespaces = SyntaxFactory.List<MemberDeclarationSyntax>
(
syntaxTree.GetRoot()
.DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.Where(m => (m.Parent as TypeDeclarationSyntax)?.AttributeLists.SelectMany(al => al.Attributes).Any(a => a.Name.ToString().StartsWith("RewriteAsync")) == true || m.AttributeLists.SelectMany(al => al.Attributes).Any(a => a.Name.ToString().StartsWith("RewriteAsync")))
.Where(c => (c.FirstAncestorOrSelf<ClassDeclarationSyntax>() as TypeDeclarationSyntax ?? c.FirstAncestorOrSelf<InterfaceDeclarationSyntax>() as TypeDeclarationSyntax) != null)
.GroupBy(m => m.FirstAncestorOrSelf<ClassDeclarationSyntax>() as TypeDeclarationSyntax ?? m.FirstAncestorOrSelf<InterfaceDeclarationSyntax>())
.GroupBy(g => g.Key.FirstAncestorOrSelf<NamespaceDeclarationSyntax>())
.Select(namespaceGrouping =>
SyntaxFactory.NamespaceDeclaration(namespaceGrouping.Key.Name)
.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>
(
namespaceGrouping.Select
(
typeGrouping =>
typeGrouping.Key is ClassDeclarationSyntax
?
SyntaxFactory.ClassDeclaration(typeGrouping.Key.Identifier).WithModifiers(typeGrouping.Key.Modifiers)
.WithTypeParameterList(typeGrouping.Key.TypeParameterList)
.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(typeGrouping.SelectMany(m => this.RewriteMethods(m, semanticModel))))
as TypeDeclarationSyntax
:
SyntaxFactory.InterfaceDeclaration(typeGrouping.Key.Identifier).WithModifiers(typeGrouping.Key.Modifiers)
.WithTypeParameterList(typeGrouping.Key.TypeParameterList)
.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(typeGrouping.SelectMany(m => this.RewriteMethods(m, semanticModel))))
as TypeDeclarationSyntax
)
))
)
);
yield return SyntaxFactory.SyntaxTree
(
SyntaxFactory.CompilationUnit()
.WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(namespaces.OfType<NamespaceDeclarationSyntax>().Select(c => AmendUsings(c, syntaxTree.GetCompilationUnitRoot().Usings))))
.WithEndOfFileToken(SyntaxFactory.Token(SyntaxKind.EndOfFileToken))
);
}
}