本文整理汇总了C#中Microsoft.CodeAnalysis.SemanticModel.LookupNamespacesAndTypes方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.LookupNamespacesAndTypes方法的具体用法?C# SemanticModel.LookupNamespacesAndTypes怎么用?C# SemanticModel.LookupNamespacesAndTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.LookupNamespacesAndTypes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculateNewRoot
private static SyntaxNode CalculateNewRoot(SyntaxNode root, ForEachStatementSyntax foreachSyntax, SemanticModel semanticModel)
{
var collection = foreachSyntax.Expression;
var typeName = foreachSyntax.Type.ToString();
var invocationToAdd = GetOfTypeInvocation(typeName, collection);
var namedTypes = semanticModel.LookupNamespacesAndTypes(foreachSyntax.SpanStart).OfType<INamedTypeSymbol>();
var isUsingAlreadyThere = namedTypes.Any(nt => nt.ToDisplayString() == ofTypeExtensionClass);
if (isUsingAlreadyThere)
{
return root
.ReplaceNode(collection, invocationToAdd)
.WithAdditionalAnnotations(Formatter.Annotation);
}
else
{
var usingDirectiveToAdd = SyntaxFactory.UsingDirective(
SyntaxFactory.QualifiedName(
SyntaxFactory.IdentifierName("System"),
SyntaxFactory.IdentifierName("Linq")));
var annotation = new SyntaxAnnotation("CollectionToChange");
var newRoot = root.ReplaceNode(
collection,
collection.WithAdditionalAnnotations(annotation));
var node = newRoot.GetAnnotatedNodes(annotation).First();
var closestNamespaceWithUsing = node.AncestorsAndSelf()
.OfType<NamespaceDeclarationSyntax>()
.FirstOrDefault(n => n.Usings.Count > 0);
if (closestNamespaceWithUsing != null)
{
newRoot = newRoot.ReplaceNode(
closestNamespaceWithUsing,
closestNamespaceWithUsing.AddUsings(usingDirectiveToAdd))
.WithAdditionalAnnotations(Formatter.Annotation);
}
else
{
var compilationUnit = node.FirstAncestorOrSelf<CompilationUnitSyntax>();
newRoot = compilationUnit.AddUsings(usingDirectiveToAdd);
}
node = newRoot.GetAnnotatedNodes(annotation).First();
return newRoot
.ReplaceNode(node, invocationToAdd)
.WithAdditionalAnnotations(Formatter.Annotation);
}
}
示例2: GetFullyQualifiedName
public override string GetFullyQualifiedName(string name, int position, SemanticModel semanticModel)
{
var typeName = SyntaxFactory.ParseTypeName(name);
if (typeName is PredefinedTypeSyntax)
{
PredefinedType predefinedType;
if (SyntaxFactsService.TryGetPredefinedType(((PredefinedTypeSyntax)typeName).Keyword, out predefinedType))
{
var specialType = predefinedType.ToSpecialType();
return semanticModel.Compilation.GetSpecialType(specialType).GetEscapedFullName();
}
}
else
{
var symbols = semanticModel.LookupNamespacesAndTypes(position, name: name);
if (symbols.Length > 0)
{
return symbols[0].GetEscapedFullName();
}
}
return name;
}
示例3: NamespaceNeedsToBeAdded
private static bool NamespaceNeedsToBeAdded(MethodDeclarationSyntax method,
SemanticModel semanticModel, string LiteralNotSupportedException, string LiteralSystem)
{
return !semanticModel.LookupNamespacesAndTypes(method.Body.CloseBraceToken.SpanStart)
.OfType<INamedTypeSymbol>()
.Any(nt =>
nt.IsType &&
nt.Name == LiteralNotSupportedException &&
nt.ContainingNamespace.Name == LiteralSystem);
}
示例4: AddImportCompletionData
public void AddImportCompletionData (CompletionResult result, Document document, SemanticModel semanticModel, int position, CancellationToken cancellationToken = default(CancellationToken))
{
var ns = new Stack<INamespaceOrTypeSymbol>();
ns.Push(semanticModel.Compilation.GlobalNamespace);
semanticModel.LookupNamespacesAndTypes(position);
}
示例5: ValidateAliasForTarget
//.........这里部分代码省略.........
// if (node is AliasQualifiedNameSyntax)
// {
// var aliasQualifiedNameNode = (AliasQualifiedNameSyntax)node;
// if (aliasQualifiedNameNode.Name.Identifier.HasAnnotations(AliasAnnotation.Kind))
// {
// aliasAnnotationInfo = aliasQualifiedNameNode.Name.Identifier.GetAnnotations(AliasAnnotation.Kind).Single();
// }
// }
//
// if (aliasAnnotationInfo != null)
// {
// var aliasName = AliasAnnotation.GetAliasName(aliasAnnotationInfo);
// var aliasIdentifier = SyntaxFactory.IdentifierName(aliasName);
//
// var aliasTypeInfo = semanticModel.GetSpeculativeAliasInfo(node.SpanStart, aliasIdentifier, SpeculativeBindingOption.BindAsTypeOrNamespace);
//
// if (aliasTypeInfo != null)
// {
// aliasReplacement = aliasTypeInfo;
// return ValidateAliasForTarget(aliasReplacement, semanticModel, node, symbol);
// }
// }
// }
//
// if (node.Kind() == SyntaxKind.IdentifierName &&
// semanticModel.GetAliasInfo((IdentifierNameSyntax)node, cancellationToken) != null)
// {
// return false;
// }
//
// // an alias can only replace a type or namespace
// if (symbol == null ||
// (symbol.Kind != SymbolKind.Namespace && symbol.Kind != SymbolKind.NamedType))
// {
// return false;
// }
//
// if (node is QualifiedNameSyntax)
// {
// var qualifiedName = (QualifiedNameSyntax)node;
// if (!qualifiedName.Right.HasAnnotation(Simplifier.SpecialTypeAnnotation))
// {
// var type = semanticModel.GetTypeInfo(node, cancellationToken).Type;
// if (type != null)
// {
// var keywordKind = GetPredefinedKeywordKind(type.SpecialType);
// if (keywordKind != SyntaxKind.None)
// {
// preferAliasToQualifiedName = false;
// }
// }
// }
// }
//
// if (node is AliasQualifiedNameSyntax)
// {
// var aliasQualifiedNameSyntax = (AliasQualifiedNameSyntax)node;
// if (!aliasQualifiedNameSyntax.Name.HasAnnotation(Simplifier.SpecialTypeAnnotation))
// {
// var type = semanticModel.GetTypeInfo(node, cancellationToken).Type;
// if (type != null)
// {
// var keywordKind = GetPredefinedKeywordKind(type.SpecialType);
// if (keywordKind != SyntaxKind.None)
// {
// preferAliasToQualifiedName = false;
// }
// }
// }
// }
//
// aliasReplacement = GetAliasForSymbol((INamespaceOrTypeSymbol)symbol, node.GetFirstToken(), semanticModel, cancellationToken);
// if (aliasReplacement != null && preferAliasToQualifiedName)
// {
// return ValidateAliasForTarget(aliasReplacement, semanticModel, node, symbol);
// }
//
// return false;
// }
//
// We must verify that the alias actually binds back to the thing it's aliasing.
// It's possible there's another symbol with the same name as the alias that binds
// first
private static bool ValidateAliasForTarget(IAliasSymbol aliasReplacement, SemanticModel semanticModel, ExpressionSyntax node, ISymbol symbol)
{
var aliasName = aliasReplacement.Name;
var boundSymbols = semanticModel.LookupNamespacesAndTypes(node.SpanStart, name: aliasName);
if (boundSymbols.Length == 1)
{
var boundAlias = boundSymbols[0] as IAliasSymbol;
if (boundAlias != null && aliasReplacement.Target.Equals(symbol))
{
return true;
}
}
return false;
}
示例6: LookupSymbol
private int LookupSymbol(SemanticModel model, int pos, INamespaceOrTypeSymbol container, string symbol)
{
Console.WriteLine("lookup: " + symbol);
//var symbols = model.LookupSymbols(0, container, symbol);
var symbols = model.LookupNamespacesAndTypes(pos, container, symbol);
foreach (var x in symbols)
Console.WriteLine(x.ToDisplayString());
return symbols.Length;
}