EnvDTE 从 CodeElement 检索数据类型
我正在使用 EnvDTE 在我的最新项目中生成一些代码。
我有一个给定 C# 类的 CodeClass 对象的引用,但现在我想循环遍历它的所有成员(在 codeClass.Members 中)并检查它们的类型。
但是,我无法设法从循环访问 codeClass.Members 时获得的 CodeElement-Object 中检索给定成员的类型。
如何检索类型(int、string 等)?
PS:反射不适合我的用例。
I am using EnvDTE to generate some code in my latest project.
I have a reference to a CodeClass-Object for a given C#-Class but now I wanted to loop through all of its members (in codeClass.Members) and check their types.
However I can't manage to retrieve the type of the given member from the CodeElement-Object that I get when looping through codeClass.Members.
How can I retrieve the type (int, string etc.)?
PS: Reflection is not an option for my usecase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CodeElement 具有 Members 属性,它是 CodeElement 的集合。 CodeElement 有一个 Kind 属性,从中您可以知道我们正在谈论的成员类型。然后您可以将每个成员转换到适当的接口并四处查看。大多数子类都有一个 Type 属性,其中包含您要查找的信息。
我在宏编辑器的模块中输入了以下内容:
它只是采用编辑器中光标所在的类,并显示任何字段或属性的类型信息。
CodeElement has the Members property, which is a collection of CodeElement. CodeElement has a Kind property, from which you can know what kind of member we're talking about. Then you can cast each member to the appropriate interface and have a look around. Most subclasses have a Type property, with the info you are looking for.
I typed this in the Macro editor, in a module:
It simply takes the class that is where the cursor is in the editor and displays the type info for any field or property.