如何将classDeclarationsyntax对象与识别符号对象进行比较?
我正在用C#编写一个源生成器,并且我有2个对象需要比较它们是否与同一类有关,但是我找不到方法。
我的第一个对象是classDeclarationsyntax
的实例。这来自我的自定义isyntaxContextreceiver
查找与特定条件匹配的类。
在我的发电机中的其他地方,我有一个disideifierNamesyntax
对象,该对象来自查看我在不同类的属性列表中找到的typefexpressyntax
中的类型。
我需要在这里比较两个对象,以查看它们是否在谈论同一件事。
使用diendifierNamesyntax
我可以使用语义模型获取类型信息:
ITypeSymbol semanticType = semanticModel.GetTypeInfo(targetType).Type;
但是我不知道如何将此itypesymbol
与classdeclarationsyntax
进行比较。
有没有办法这样做,或者有没有办法获得classDeclarationsyntax
对象的语义模型类型信息?
I'm writing a source generator in C#, and I've got 2 objects that I need to compare to see if they relate to the same class, but I can't find a way to do it.
My first object is an instance of ClassDeclarationSyntax
. This is coming from my custom ISyntaxContextReceiver
to find classes that match specific conditions.
Elsewhere in my generator I have an IdentifierNameSyntax
object, which is coming from looking at the types within a TypeOfExpressionSyntax
that I find within a different class's list of attributes.
I need to compare the two objects here to see if they are talking about the same thing.
With the IdentifierNameSyntax
I can get the type information by using the semantic model:
ITypeSymbol semanticType = semanticModel.GetTypeInfo(targetType).Type;
But I don't know how to compare this ITypeSymbol
against ClassDeclarationSyntax
either.
Is there a way to do this, or is there a way to get the semantic model type information for a ClassDeclarationSyntax
object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的方法是
getDeclaredSymbol
onsemanticmodel
。如您可以从文档中看到,有大量的过载可以使您不仅可以获取类,而且还可以获取类,字段,方法,属性,事件,参数等的关联符号信息。绝对要保留在后袋中的方法!The method you are looking for is
GetDeclaredSymbol
onsemanticModel
. As you can see from the documentation, there are huge number of overloads which will allow you to get the associated symbol information for not only classes, but also fields, methods, properties, events, parameters, and so forth. Definitely a method you'll want to keep in your back pocket!