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


C# Compilation.AddSyntaxTrees方法代码示例

本文整理汇总了C#中Compilation.AddSyntaxTrees方法的典型用法代码示例。如果您正苦于以下问题:C# Compilation.AddSyntaxTrees方法的具体用法?C# Compilation.AddSyntaxTrees怎么用?C# Compilation.AddSyntaxTrees使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Compilation的用法示例。


在下文中一共展示了Compilation.AddSyntaxTrees方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetTypeSymbolFromFullName

        public override ITypeSymbol GetTypeSymbolFromFullName(string fullName, Compilation compilation)
        {
            ITypeSymbol typeSymbol = compilation.GetTypeByMetadataName(fullName);

            if (typeSymbol == null)
            {
                var parsedTypeName = SyntaxFactory.ParseTypeName(fullName);

                // Check to see if the name we parsed has any skipped text. If it does, don't bother trying to
                // speculatively bind it because we'll likely just get the wrong thing since we found a bunch
                // of non-sensical tokens.

                if (parsedTypeName.ContainsSkippedText)
                {
                    return null;
                }

                // If we couldn't get the name, we just grab the first tree in the compilation to
                // speculatively bind at position zero. However, if there *aren't* any trees, we fork the
                // compilation with an empty tree for the purposes of speculative binding.
                //
                // I'm a bad person.

                var tree = compilation.SyntaxTrees.FirstOrDefault();
                if (tree == null)
                {
                    tree = SyntaxFactory.ParseSyntaxTree("");
                    compilation = compilation.AddSyntaxTrees(tree);
                }

                var semanticModel = compilation.GetSemanticModel(tree);
                typeSymbol = semanticModel.GetSpeculativeTypeInfo(0, parsedTypeName, SpeculativeBindingOption.BindAsTypeOrNamespace).Type;
            }

            if (typeSymbol == null)
            {
                Debug.Fail("Could not find type: " + fullName);
                throw new ArgumentException();
            }

            return typeSymbol;
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:42,代码来源:CSharpCodeModelService.cs

示例2: GetTypeSymbolFromFullName

        public override ITypeSymbol GetTypeSymbolFromFullName(string fullName, Compilation compilation)
        {
            ITypeSymbol typeSymbol = compilation.GetTypeByMetadataName(fullName);

            if (typeSymbol == null)
            {
                var parsedTypeName = SyntaxFactory.ParseTypeName(fullName);

                // If we couldn't get the name, we just grab the first tree in the compilation to
                // speculatively bind at position zero. However, if there *aren't* any trees, we fork the
                // compilation with an empty tree for the purposes of speculative binding.
                //
                // I'm a bad person.

                var tree = compilation.SyntaxTrees.FirstOrDefault();
                if (tree == null)
                {
                    tree = SyntaxFactory.ParseSyntaxTree("");
                    compilation = compilation.AddSyntaxTrees(tree);
                }

                var semanticModel = compilation.GetSemanticModel(tree);
                typeSymbol = semanticModel.GetSpeculativeTypeInfo(0, parsedTypeName, SpeculativeBindingOption.BindAsTypeOrNamespace).Type;
            }

            if (typeSymbol == null)
            {
                Debug.Fail("Could not find type: " + fullName);
                throw new ArgumentException();
            }

            return typeSymbol;
        }
开发者ID:nemec,项目名称:roslyn,代码行数:33,代码来源:CSharpCodeModelService.cs

示例3: AddToCompilation

		private static Tuple<Compilation, SyntaxTree> AddToCompilation(Compilation compilation, SyntaxTree tree)
		{
			if (!compilation.ContainsSyntaxTree(tree))
			{
				var newTree = tree;
				if (!tree.HasCompilationUnitRoot)
				{
					var childNodes = tree.GetRoot()
						.ChildNodes()
						.AsArray();
					newTree = CSharpSyntaxTree.Create(SyntaxFactory.CompilationUnit()
						.WithMembers(
							SyntaxFactory.List(childNodes.OfType<MemberDeclarationSyntax>()))
						.WithUsings(
							SyntaxFactory.List(childNodes.OfType<UsingDirectiveSyntax>()))
						.WithExterns(
							SyntaxFactory.List(childNodes.OfType<ExternAliasDirectiveSyntax>())));
				}

				var comp = compilation.AddSyntaxTrees(newTree);
				return new Tuple<Compilation, SyntaxTree>(comp, newTree);
			}

			return new Tuple<Compilation, SyntaxTree>(compilation, tree);
		}
开发者ID:henrylle,项目名称:ArchiMetrics,代码行数:25,代码来源:CodeMetricsCalculator.cs

示例4: VerifyCompilation

		private static async Task<Tuple<Compilation, SemanticModel, SyntaxTree, NamespaceDeclarationSyntaxInfo>> VerifyCompilation(Compilation compilation, NamespaceDeclarationSyntaxInfo namespaceNode)
		{
			SemanticModel semanticModel;
			var tree = namespaceNode.Syntax.SyntaxTree;
			if (tree == null)
			{
				var compilationUnit = SyntaxFactory.CompilationUnit()
					.WithMembers(SyntaxFactory.List(new[] { (MemberDeclarationSyntax)namespaceNode.Syntax }));
				var cu = CSharpSyntaxTree.Create(compilationUnit);
				var root = await cu.GetRootAsync().ConfigureAwait(false);
				namespaceNode.Syntax = root.ChildNodes().First();
				var newCompilation = compilation.AddSyntaxTrees(cu);
				semanticModel = newCompilation.GetSemanticModel(cu);
				return new Tuple<Compilation, SemanticModel, SyntaxTree, NamespaceDeclarationSyntaxInfo>(newCompilation, semanticModel, cu, namespaceNode);
			}

			var result = AddToCompilation(compilation, tree);
			compilation = result.Item1;
			tree = result.Item2;
			semanticModel = compilation.GetSemanticModel(tree);
			return new Tuple<Compilation, SemanticModel, SyntaxTree, NamespaceDeclarationSyntaxInfo>(compilation, semanticModel, tree, namespaceNode);
		}
开发者ID:henrylle,项目名称:ArchiMetrics,代码行数:22,代码来源:CodeMetricsCalculator.cs


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