locked
List all available objects in VS 2010 extension RRS feed

  • Question

  • Hi! I need a hint for the following: I want to write an extension for VS 2010 which needs a list of all available objects (types; classes with fields, properties, methods etc.). Since I need the data at design time, I can't and don't want to use reflection. Roughly speaking, I need a kind of IntelliSense data or data for a solution wide code map. Is there an elegant way to achieve this? Thanks a lot. micromirko
    • Moved by Mike Feng Thursday, December 8, 2011 9:23 AM VSX (From:Visual Studio Class Designer)
    Wednesday, December 7, 2011 12:49 AM

Answers

  • Hi Micromirko,

    You can try to write a macros to list all available objects.

    For example, you can use the following code to list all classes and namespace in the project.

    ·         ' Macro editor

    ·         Sub FindAllClasses()

    ·            Dim cm As CodeModel

    ·            cm = DTE.Solution.Projects.Item(1).CodeModel

    ·          

    ·            ' Look for all the namespaces and classes in the

    ·            ' project.

    ·            Dim list As String

    ·            Dim ce As CodeElement

    ·            For Each ce In cm.CodeElements

    ·               If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then

    ·                  ' Determine whether that namespace or class

    ·                  ' contains other classes.

    ·                  GetClass(ce, list)

    ·               End If

    ·            Next

    ·          

    ·            MsgBox(list)

    ·         End Sub

    ·          

    ·         Sub GetClass(ByVal ct As CodeElement, ByRef list As String)

    ·            ' ct could be a namespace or a class. Add it to the list

    ·            ' if it is a class.

    ·            If (TypeOf ct Is CodeClass) Then

    ·               list &= ct.Name & ControlChars.CrLf

    ·            End If

    ·          

    ·            ' Determine whether there are nested namespaces or classes that

    ·            ' might contain other classes.

    ·            Dim aspace As CodeNamespace

    ·            Dim ce As CodeElement

    ·            Dim cn As CodeNamespace

    ·            Dim cc As CodeClass

    ·            Dim elements As CodeElements

    ·            If (TypeOf ct Is CodeNamespace) Then

    ·               cn = CType(ct, CodeNamespace)

    ·               elements = cn.Members

    ·            Else

    ·               cc = CType(ct, CodeClass)

    ·               elements = cc.Members

    ·            End If

    ·          

    ·            For Each ce In elements

    ·               If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then

    ·                  GetClass(ce, list)

    ·               End If

    ·            Next

    ·         End Sub

     If you also want to list all methods and properties, you just need  to use CodeFunction and CodeProperty as it type.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    • Proposed as answer by Yi Feng Li Tuesday, January 10, 2012 5:48 AM
    • Marked as answer by lucy-liu Tuesday, January 10, 2012 5:50 AM
    Thursday, December 8, 2011 10:01 AM
  • The CodeModel class is the only supported scenario right now. The various languages don't expose their internal structures (AST) etc. So it's quite difficult to determine this info. Ultimately, you might have to resort to parsing the sources files yourself. One interesting scenario (still in development) is the "Roslyn" project that may give you some additional options. Currently, there is a CTP release available. It's specific to C# and VB .Net, and still under development (slated to be released sometime after the next version of Visual Studio).

    http://social.msdn.microsoft.com/Forums/en-US/roslyn/threads

    Sincerely,


    Ed Dore
    • Marked as answer by lucy-liu Monday, January 9, 2012 3:20 AM
    Thursday, December 15, 2011 5:16 AM

All replies

  • Hi MicroMirko,

    Welcome to the MSDN Forum.

    I have moved this thread to a dedicated forum for better support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, December 8, 2011 9:22 AM
  • Hi Micromirko,

    You can try to write a macros to list all available objects.

    For example, you can use the following code to list all classes and namespace in the project.

    ·         ' Macro editor

    ·         Sub FindAllClasses()

    ·            Dim cm As CodeModel

    ·            cm = DTE.Solution.Projects.Item(1).CodeModel

    ·          

    ·            ' Look for all the namespaces and classes in the

    ·            ' project.

    ·            Dim list As String

    ·            Dim ce As CodeElement

    ·            For Each ce In cm.CodeElements

    ·               If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then

    ·                  ' Determine whether that namespace or class

    ·                  ' contains other classes.

    ·                  GetClass(ce, list)

    ·               End If

    ·            Next

    ·          

    ·            MsgBox(list)

    ·         End Sub

    ·          

    ·         Sub GetClass(ByVal ct As CodeElement, ByRef list As String)

    ·            ' ct could be a namespace or a class. Add it to the list

    ·            ' if it is a class.

    ·            If (TypeOf ct Is CodeClass) Then

    ·               list &= ct.Name & ControlChars.CrLf

    ·            End If

    ·          

    ·            ' Determine whether there are nested namespaces or classes that

    ·            ' might contain other classes.

    ·            Dim aspace As CodeNamespace

    ·            Dim ce As CodeElement

    ·            Dim cn As CodeNamespace

    ·            Dim cc As CodeClass

    ·            Dim elements As CodeElements

    ·            If (TypeOf ct Is CodeNamespace) Then

    ·               cn = CType(ct, CodeNamespace)

    ·               elements = cn.Members

    ·            Else

    ·               cc = CType(ct, CodeClass)

    ·               elements = cc.Members

    ·            End If

    ·          

    ·            For Each ce In elements

    ·               If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then

    ·                  GetClass(ce, list)

    ·               End If

    ·            Next

    ·         End Sub

     If you also want to list all methods and properties, you just need  to use CodeFunction and CodeProperty as it type.

     

    Best regards,

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    • Proposed as answer by Yi Feng Li Tuesday, January 10, 2012 5:48 AM
    • Marked as answer by lucy-liu Tuesday, January 10, 2012 5:50 AM
    Thursday, December 8, 2011 10:01 AM
  • Thanks Mike! Was not sure where to post it, and I did not find that category. :)

    Thursday, December 8, 2011 11:20 PM
  • Hello Lucy,

    thanks for the detailed answer. I tried it this way, but I thought that there was a better build-in solution. Tried also to use the Derived-Member of the CodeClass, which unfortunately is not implemented (yet).

    The point is, that I also want the types, classes etc. from all referenced assemblies, so I guess I really have to use a mixture of reflection and CM.

    But if there is a better solution, please let me know!

     

    Thanks,

       Micromirko

    Thursday, December 8, 2011 11:30 PM
  • could you share your solutions in detailed?

    Wednesday, December 14, 2011 10:10 AM
  • The CodeModel class is the only supported scenario right now. The various languages don't expose their internal structures (AST) etc. So it's quite difficult to determine this info. Ultimately, you might have to resort to parsing the sources files yourself. One interesting scenario (still in development) is the "Roslyn" project that may give you some additional options. Currently, there is a CTP release available. It's specific to C# and VB .Net, and still under development (slated to be released sometime after the next version of Visual Studio).

    http://social.msdn.microsoft.com/Forums/en-US/roslyn/threads

    Sincerely,


    Ed Dore
    • Marked as answer by lucy-liu Monday, January 9, 2012 3:20 AM
    Thursday, December 15, 2011 5:16 AM