如何将classDeclarationsyntax对象与识别符号对象进行比较?

发布于 2025-01-28 12:32:25 字数 682 浏览 3 评论 0原文

我正在用C#编写一个源生成器,并且我有2个对象需要比较它们是否与同一类有关,但是我找不到方法。

我的第一个对象是classDeclarationsyntax的实例。这来自我的自定义isyntaxContextreceiver查找与特定条件匹配的类。

在我的发电机中的其他地方,我有一个disideifierNamesyntax对象,该对象来自查看我在不同类的属性列表中找到的typefexpressyntax中的类型。

我需要在这里比较两个对象,以查看它们是否在谈论同一件事。

使用diendifierNamesyntax我可以使用语义模型获取类型信息:

ITypeSymbol semanticType = semanticModel.GetTypeInfo(targetType).Type;

但是我不知道如何将此itypesymbolclassdeclarationsyntax进行比较。

有没有办法这样做,或者有没有办法获得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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2025-02-04 12:32:25

您正在寻找的方法是getDeclaredSymbol on semanticmodel。如您可以从文档中看到,有大量的过载可以使您不仅可以获取类,而且还可以获取类,字段,方法,属性,事件,参数等的关联符号信息。绝对要保留在后袋中的方法!

The method you are looking for is GetDeclaredSymbol on semanticModel. 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文