何时使用 SemanticModel.GetSymbolInfo 以及何时使用 SemanticModel.GetDeclaredSymbol [英] When to use SemanticModel.GetSymbolInfo and when SemanticModel.GetDeclaredSymbol

查看:33
本文介绍了何时使用 SemanticModel.GetSymbolInfo 以及何时使用 SemanticModel.GetDeclaredSymbol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,当我尝试为我的语法节点获取 ISymbol 时,我在使用 SemanticModel.GetSymbolInfo 时失败(获取 null)但在使用 SemanticModel.GetDeclaredSymbol 时成功.

In some cases, when I'm trying to get the the ISymbol for my syntax node, I'm fail (getting null) when using SemanticModel.GetSymbolInfo but succeed when using SemanticModel.GetDeclaredSymbol.

我在下面附上了一个例子.

I've attached an example bellow.

所以我的问题是什么时候使用每种方法来获取语义模型?

So my question is when to use each one of the methods for getting the semantic model?

public class Class1
{
    public System.String MyString { get; set; }

    public static void Main()
    {
        var str =
            @"
            namespace ClassLibrary31
            {
                public class Class1
                {
                    public System.String MyString { get; set; }
                }
            }";

        var syntaxTree = SyntaxFactory.ParseSyntaxTree(str);

        MetadataReference[] metadataReferenceReferences = new MetadataReference[]
        {
            MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
        };

        var compilation =
            CSharpCompilation
                .Create("TraceFluent",
                    new[] {syntaxTree},
                    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, warningLevel:1),
                    references: metadataReferenceReferences
                );

        var temp = compilation.Emit("temp");
        var semanticModel = compilation.GetSemanticModel(syntaxTree, true);

        PropertyDeclarationSyntax propertySyntaxNode = 
            syntaxTree.GetRoot()
                .DescendantNodes()
                .OfType<PropertyDeclarationSyntax>()
                .First();



        //var qu = propertySyntaxNode.q

        //var symbolInfo = semanticModel.GetDeclaredSymbol(propertySyntaxNode);
        var symbol = semanticModel.GetDeclaredSymbol(propertySyntaxNode) as IPropertySymbol;
        var typeInfo = semanticModel.GetTypeInfo(propertySyntaxNode).Type;
    }
}

推荐答案

我相信您的意思是获取给定语法节点的符号,而不是获取树的语义模型.

I believe you mean getting the symbol for a given syntax node, and not getting the semantic model for the tree.

通常,当您想要获取声明的底层符号(类、属性、方法等)时,您应该使用 GetDeclaredSymbol.在内部,GetSymbolInfo 调用 this 方法.您可以看到在那里处理的不同案例.不处理声明,因此对于那些您需要使用 GetDeclaredSymbol 的人,您可以找到其内部结构 这里.

Generally, when you want to get the underlying symbol of a declaration (class, property, method, ...), then you should use the GetDeclaredSymbol. Internally, GetSymbolInfo calls this method. You can see the different cases handled there. Declarations are not handled, so for those you'd need to use GetDeclaredSymbol, whose internals you can find here.

这篇关于何时使用 SemanticModel.GetSymbolInfo 以及何时使用 SemanticModel.GetDeclaredSymbol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆