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.