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


C# CSharpCompilation.GetSemanticModel方法代码示例

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


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

示例1: CheckCompilationSyntaxTrees

        private static void CheckCompilationSyntaxTrees(CSharpCompilation compilation, params SyntaxTree[] expectedSyntaxTrees)
        {
            ImmutableArray<SyntaxTree> actualSyntaxTrees = compilation.SyntaxTrees;

            int numTrees = expectedSyntaxTrees.Length;

            Assert.Equal(numTrees, actualSyntaxTrees.Length);
            for (int i = 0; i < numTrees; i++)
            {
                Assert.Equal(expectedSyntaxTrees[i], actualSyntaxTrees[i]);
            }

            for (int i = 0; i < numTrees; i++)
            {
                for (int j = 0; j < numTrees; j++)
                {
                    Assert.Equal(Math.Sign(compilation.CompareSyntaxTreeOrdering(expectedSyntaxTrees[i], expectedSyntaxTrees[j])), Math.Sign(i.CompareTo(j)));
                }
            }

            var types = expectedSyntaxTrees.Select(tree => compilation.GetSemanticModel(tree).GetDeclaredSymbol(tree.GetCompilationUnitRoot().Members.Single())).ToArray();
            for (int i = 0; i < numTrees; i++)
            {
                for (int j = 0; j < numTrees; j++)
                {
                    Assert.Equal(Math.Sign(compilation.CompareSourceLocations(types[i].Locations[0], types[j].Locations[0])), Math.Sign(i.CompareTo(j)));
                }
            }
        }
开发者ID:ehsansajjad465,项目名称:roslyn,代码行数:29,代码来源:CompilationCreationTests.cs

示例2: GetSpeculativeType

 private static TypeSymbol GetSpeculativeType(CSharpCompilation comp, string name)
 {
     var tree = comp.SyntaxTrees.Single();
     var model = comp.GetSemanticModel(tree);
     return (TypeSymbol)model.GetSpeculativeTypeInfo(
         tree.Length, 
         SyntaxFactory.IdentifierName(name), 
         SpeculativeBindingOption.BindAsTypeOrNamespace).Type;
 }
开发者ID:krishnarajbb,项目名称:roslyn,代码行数:9,代码来源:InteractiveUsingTests.cs

示例3: GetSpeculativeSymbol

 private static Symbol GetSpeculativeSymbol(CSharpCompilation comp, string name)
 {
     var tree = comp.SyntaxTrees.Single();
     var model = comp.GetSemanticModel(tree);
     return (Symbol)model.GetSpeculativeSymbolInfo(
         tree.Length,
         SyntaxFactory.IdentifierName(name),
         SpeculativeBindingOption.BindAsExpression).Symbol;
 }
开发者ID:krishnarajbb,项目名称:roslyn,代码行数:9,代码来源:InteractiveUsingTests.cs

示例4: VerifySemanticInfoForLockStatements

        private static void VerifySemanticInfoForLockStatements(CSharpCompilation compilation, ISymbol symbol, int index = 1, bool isSymbolNull = false)
        {
            var tree = compilation.SyntaxTrees.Single();
            var model = compilation.GetSemanticModel(tree);

            var lockStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<LockStatementSyntax>().ToList();
            var symbolInfo = model.GetSymbolInfo(lockStatements[index - 1].Expression);

            if (isSymbolNull == true)
            {
                Assert.Null(symbolInfo.Symbol);
            }
            else
            {
                Assert.NotNull(symbolInfo.Symbol);
            }
            var typeInfo = model.GetTypeInfo(lockStatements[index - 1].Expression);
            Assert.NotNull(typeInfo.Type);
            Assert.NotNull(typeInfo.ConvertedType);

            Assert.Equal(symbol, (TypeSymbol)typeInfo.Type);
            Assert.Equal(symbol, (TypeSymbol)typeInfo.ConvertedType);
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:23,代码来源:LockTests.cs

示例5: VerifyLookUpSymbolForUsingStatements

        private ISymbol VerifyLookUpSymbolForUsingStatements(CSharpCompilation compilation, Symbol symbol, int index = 1)
        {
            var tree = compilation.SyntaxTrees.Single();
            var model = compilation.GetSemanticModel(tree);
            var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();

            var actualSymbol = model.LookupSymbols(usingStatements[index - 1].SpanStart, name: symbol.Name).Single();
            Assert.Equal(SymbolKind.Local, actualSymbol.Kind);
            Assert.Equal(symbol, actualSymbol);
            return actualSymbol;
        }
开发者ID:gustavoasoares,项目名称:roslyn,代码行数:11,代码来源:UsingStatementTests.cs

示例6: VerifySymbolInfoForUsingStatements

        private SymbolInfo VerifySymbolInfoForUsingStatements(CSharpCompilation compilation, Symbol symbol, int index = 1)
        {
            var tree = compilation.SyntaxTrees.Single();
            var model = compilation.GetSemanticModel(tree);

            var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();

            var type = model.GetSymbolInfo(usingStatements[index - 1].Declaration.Type);

            Assert.Equal(symbol, type.Symbol);

            return type;
        }
开发者ID:gustavoasoares,项目名称:roslyn,代码行数:13,代码来源:UsingStatementTests.cs

示例7: VerifyDeclaredSymbolForUsingStatements

        private IEnumerable VerifyDeclaredSymbolForUsingStatements(CSharpCompilation compilation, int index = 1, params string[] variables)
        {
            var tree = compilation.SyntaxTrees.Single();
            var model = compilation.GetSemanticModel(tree);

            var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();
            var i = 0;
            foreach (var x in usingStatements[index - 1].Declaration.Variables)
            {
                var symbol = model.GetDeclaredSymbol(x);
                Assert.Equal(SymbolKind.Local, symbol.Kind);
                Assert.Equal(variables[i++].ToString(), symbol.ToDisplayString());
                yield return symbol;
            }
        }
开发者ID:gustavoasoares,项目名称:roslyn,代码行数:15,代码来源:UsingStatementTests.cs

示例8: GetUsingStatements

 private UsingStatementSyntax GetUsingStatements(CSharpCompilation compilation, int index = 1)
 {
     var tree = compilation.SyntaxTrees.Single();
     var model = compilation.GetSemanticModel(tree);
     var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();
     return usingStatements[index - 1];
 }
开发者ID:gustavoasoares,项目名称:roslyn,代码行数:7,代码来源:UsingStatementTests.cs

示例9: TestSemanticModelAPI

        private static void TestSemanticModelAPI(
            CSharpCompilation compilation, 
            SyntaxTree tree, 
            ImmutableArray<SemanticModelInfo> infos, 
            ImmutableArray<Diagnostic> diagnostics, 
            bool backwards)
        {
            SemanticModel semanticModel = compilation.GetSemanticModel(tree);

            SymbolInfo symInfo;
            TypeInfo typeInfo;

            foreach (var info in backwards ? infos.Reverse() : infos)
            {
                info.Clear();

                switch (info.Node.Kind)
                {
                    case SyntaxKind.Parameter:
                        var parameterDecl = (ParameterSyntax)info.Node;
                        var param = (ParameterSymbol)semanticModel.GetDeclaredSymbol(parameterDecl);

                        Assert.Equal(parameterDecl.Identifier.ValueText, param.Name);
                        Assert.Equal(parameterDecl.Identifier.GetLocation(), param.Locations[0]);

                        info.SymInfo = new SymbolInfo(param);
                        break;

                    case SyntaxKind.VariableDeclarator:
                        var declarator = (VariableDeclaratorSyntax)info.Node;
                        var local = (LocalSymbol)semanticModel.GetDeclaredSymbol(declarator);

                        if ((object)local != null)
                        {
                            Assert.Equal(declarator.Identifier.ValueText, local.Name);
                            Assert.Equal(declarator.Identifier.GetLocation(), local.Locations[0]);
                        }
                        else if (declarator.Identifier.IsMissing)
                        {
                            break;
                        }
                        else
                        {
                            Assert.Equal(SyntaxKind.DeclarationExpression, declarator.Parent.Kind);
                            Assert.True(diagnostics.Where(d => d.Code == (int)ErrorCode.ERR_DeclarationExpressionOutOfContext &&
                                                          d.Location == declarator.Parent.GetLocation()).Any());
                            break;
                        }

                        info.SymInfo = new SymbolInfo(local);

                        if (declarator.Parent.Kind == SyntaxKind.DeclarationExpression)
                        {
                            Assert.Equal(LocalDeclarationKind.RegularVariable, local.DeclarationKind);

                            var declExpr = (DeclarationExpressionSyntax)declarator.Parent;
                            symInfo = semanticModel.GetSymbolInfo(declExpr);
                            Assert.Same(local, symInfo.Symbol);

                            typeInfo = semanticModel.GetTypeInfo(declExpr);
                            Assert.Equal(local.Type, typeInfo.Type);
                        }

                        Assert.True(semanticModel.LookupNames(declarator.Identifier.Position).Contains(local.Name));

                        var declDiagnostics = diagnostics.Where(d => d.Location == declarator.Identifier.GetLocation()).ToArray();

                        bool duplicate = declDiagnostics.Where(d => d.Code == (int)ErrorCode.ERR_LocalDuplicate).Any();
                        bool overrides = declDiagnostics.Where(d => d.Code == (int)ErrorCode.ERR_LocalIllegallyOverrides).Any();

                        Assert.False(duplicate && overrides);

                        if (duplicate)
                        {
                            Assert.NotEqual(local, semanticModel.LookupSymbols(declarator.Identifier.Position, name: local.Name).Single());
                        }
                        else
                        {
                            Assert.Same(local, semanticModel.LookupSymbols(declarator.Identifier.Position, name: local.Name).Single());
                        }

                        break;

                    case SyntaxKind.CatchDeclaration:
                        var catchDecl = (CatchDeclarationSyntax)info.Node;
                        local = (LocalSymbol)semanticModel.GetDeclaredSymbol(catchDecl);
                        Assert.Equal(catchDecl.Identifier.ValueText, local.Name);
                        Assert.Equal(catchDecl.Identifier.GetLocation(), local.Locations[0]);

                        info.SymInfo = new SymbolInfo(local);
                        break;

                    case SyntaxKind.ForEachStatement:
                        var loop = (ForEachStatementSyntax)info.Node;
                        local = (LocalSymbol)semanticModel.GetDeclaredSymbol(loop);
                        Assert.Equal(loop.Identifier.ValueText, local.Name);
                        Assert.Equal(loop.Identifier.GetLocation(), local.Locations[0]);

                        info.SymInfo = new SymbolInfo(local);
                        break;
//.........这里部分代码省略.........
开发者ID:jerriclynsjohn,项目名称:roslyn,代码行数:101,代码来源:DeclarationExpressionsTests.cs


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