当前位置: 首页>>代码示例>>C#>>正文


C# SemanticModel.LookupNamespacesAndTypes方法代码示例

本文整理汇总了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);
            }
        }
开发者ID:Azzhag,项目名称:sonarlint-vs,代码行数:50,代码来源:ForeachLoopExplicitConversionCodeFixProvider.cs

示例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;
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:23,代码来源:CSharpCodeModelService.cs

示例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);
 }
开发者ID:jakobehn,项目名称:sonarlint-vs,代码行数:10,代码来源:EmptyMethodCodeFixProvider.cs

示例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);
		}
开发者ID:swarkcn,项目名称:monodevelop,代码行数:7,代码来源:CompletionEngine.cs

示例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;
        }
开发者ID:ChrisCross67,项目名称:RefactoringEssentials,代码行数:101,代码来源:ExpressionSyntaxExtensions.cs

示例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;
 }
开发者ID:nul800sebastiaan,项目名称:Zbu.ModelsBuilder,代码行数:9,代码来源:RoslynTests.cs


注:本文中的Microsoft.CodeAnalysis.SemanticModel.LookupNamespacesAndTypes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。