none
Get Class Name and Available methods name in the class

    Question

  • Hi,

    I am integrating visual studio using VSIP. Is there any way to to get the class name and methods name available in that class for any Source code file(Active Document).

    Thnaks.
    AKB
    Friday, March 12, 2010 3:37 AM

Answers

  • Hi AKB,

    Yes, you can get the class name and method name in active document. But VC is different from VB.NET and C#. For VC project type, please refer to following code:

    EnvDTE80.DTE2 _applicationObject = (EnvDTE80.DTE2)GetService(typeof(SDTE));
    VCFileCodeModel fcm = _applicationObject.ActiveDocument.ProjectItem.FileCodeModel as VCFileCodeModel;
    for (int i = 1; i <= fcm.Classes.Count; i++)
    {
        VCCodeClass c1 = fcm.Classes.Item(i) as VCCodeClass;
        MessageBox.Show(c1.Name);
    }
    for (int i = 1; i <= fcm.Functions.Count; i++)
    {
        VCCodeFunction fcn = fcm.Functions.Item(i) as VCCodeFunction;
        MessageBox.Show(fcn.Name);
    }
    Please don't forget add Microsoft.VisualStudio.VCCodeModel, EnvDTE and EnvDTE80 as reference. For VB.NET and C# project type, please see:

    EnvDTE80.DTE2 _applicationObject = (EnvDTE80.DTE2)GetService(typeof(SDTE));
    FileCodeModel fcm = _applicationObject.ActiveDocument.ProjectItem.FileCodeModel as FileCodeModel;
    foreach (CodeElement element in fcm.CodeElements)
    {
        if (element is CodeNamespace)
        {
            CodeNamespace nsp = element as CodeNamespace;
    
            foreach (CodeElement subElement in nsp.Children)
            {
                if (subElement is CodeClass)
                {
                    CodeClass c2 = subElement as CodeClass;
                    MessageBox.Show(c2.Name);
                }
            }
        }
    }
    These code does not fully meet your requirements, you can refer to it and write your own code to achieve your requirements.

    If I misunderstood you, or you have any comments, please let me know.

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by A.K.B. _ Wednesday, March 17, 2010 1:21 PM
    Monday, March 15, 2010 7:00 AM

All replies

  • Hi AKB,

    Yes, you can get the class name and method name in active document. But VC is different from VB.NET and C#. For VC project type, please refer to following code:

    EnvDTE80.DTE2 _applicationObject = (EnvDTE80.DTE2)GetService(typeof(SDTE));
    VCFileCodeModel fcm = _applicationObject.ActiveDocument.ProjectItem.FileCodeModel as VCFileCodeModel;
    for (int i = 1; i <= fcm.Classes.Count; i++)
    {
        VCCodeClass c1 = fcm.Classes.Item(i) as VCCodeClass;
        MessageBox.Show(c1.Name);
    }
    for (int i = 1; i <= fcm.Functions.Count; i++)
    {
        VCCodeFunction fcn = fcm.Functions.Item(i) as VCCodeFunction;
        MessageBox.Show(fcn.Name);
    }
    Please don't forget add Microsoft.VisualStudio.VCCodeModel, EnvDTE and EnvDTE80 as reference. For VB.NET and C# project type, please see:

    EnvDTE80.DTE2 _applicationObject = (EnvDTE80.DTE2)GetService(typeof(SDTE));
    FileCodeModel fcm = _applicationObject.ActiveDocument.ProjectItem.FileCodeModel as FileCodeModel;
    foreach (CodeElement element in fcm.CodeElements)
    {
        if (element is CodeNamespace)
        {
            CodeNamespace nsp = element as CodeNamespace;
    
            foreach (CodeElement subElement in nsp.Children)
            {
                if (subElement is CodeClass)
                {
                    CodeClass c2 = subElement as CodeClass;
                    MessageBox.Show(c2.Name);
                }
            }
        }
    }
    These code does not fully meet your requirements, you can refer to it and write your own code to achieve your requirements.

    If I misunderstood you, or you have any comments, please let me know.

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by A.K.B. _ Wednesday, March 17, 2010 1:21 PM
    Monday, March 15, 2010 7:00 AM
  • Thanks Nancy,It worked.
    Wednesday, March 17, 2010 1:21 PM