EnvDTE 从 CodeElement 检索数据类型

发布于 2024-09-04 07:08:41 字数 249 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

风蛊 2024-09-11 07:08:41

CodeElement 具有 Members 属性,它是 CodeElement 的集合。 CodeElement 有一个 Kind 属性,从中您可以知道我们正在谈论的成员类型。然后您可以将每个成员转换到适当的接口并四处查看。大多数子类都有一个 Type 属性,其中包含您要查找的信息。

我在宏编辑器的模块中输入了以下内容:

Public Sub DisplayStuff()

    Dim objTextSel As TextSelection
    Dim objCodeCls As CodeClass
    objTextSel = CType(DTE.ActiveDocument.Selection, TextSelection)
    objCodeCls = CType(objTextSel.ActivePoint.CodeElement(vsCMElement.vsCMElementClass), CodeClass)

    If objCodeCls Is Nothing Then
        MsgBox("Please launch this macro when the cursor is within a class")
        Exit Sub
    End If

    For Each elt As CodeElement2 In objCodeCls.Members

        Select Case elt.Kind

            Case vsCMElement.vsCMElementVariable

                Dim v As CodeVariable2 = CType(elt, CodeVariable2)

                MsgBox(v.Name & " is a variable of type " & v.Type.AsString)

            Case vsCMElement.vsCMElementProperty

                Dim p As CodeProperty2 = CType(elt, CodeProperty2)

                MsgBox(p.Name & " is of type " & p.Type.AsString)
        End Select


    Next
End Sub

它只是采用编辑器中光标所在的类,并显示任何字段或属性的类型信息。

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:

Public Sub DisplayStuff()

    Dim objTextSel As TextSelection
    Dim objCodeCls As CodeClass
    objTextSel = CType(DTE.ActiveDocument.Selection, TextSelection)
    objCodeCls = CType(objTextSel.ActivePoint.CodeElement(vsCMElement.vsCMElementClass), CodeClass)

    If objCodeCls Is Nothing Then
        MsgBox("Please launch this macro when the cursor is within a class")
        Exit Sub
    End If

    For Each elt As CodeElement2 In objCodeCls.Members

        Select Case elt.Kind

            Case vsCMElement.vsCMElementVariable

                Dim v As CodeVariable2 = CType(elt, CodeVariable2)

                MsgBox(v.Name & " is a variable of type " & v.Type.AsString)

            Case vsCMElement.vsCMElementProperty

                Dim p As CodeProperty2 = CType(elt, CodeProperty2)

                MsgBox(p.Name & " is of type " & p.Type.AsString)
        End Select


    Next
End Sub

It simply takes the class that is where the cursor is in the editor and displays the type info for any field or property.

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