private void MenuItemCallback(object sender, EventArgs e)
{
EnvDTE.DTE dte;
dte = this.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
ShowCodeElements(dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements);
}
private void ShowCodeElements(EnvDTE.CodeElements codeElements)
{
foreach (EnvDTE.CodeElement codeElement in codeElements)
{
switch (codeElement)
{
case EnvDTE.CodeNamespace codeNamespace:
ShowCodeElements(codeNamespace.Members);
break;
case EnvDTE.CodeType codeType:
ShowCodeElements(codeType.Members);
break;
case EnvDTE.CodeProperty codeProperty:
GetPoints(codeElement);
break;
case EnvDTE.CodeFunction codeFunction:
GetPoints(codeElement);
break;
}
}
}
private void GetPoints(EnvDTE.CodeElement codeElement)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine(codeElement.Name);
sb.AppendLine();
GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartWholeWithAttributes);
GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartWhole);
GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartHeaderWithAttributes);
GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartHeader);
GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartName);
GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartBody);
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
sb.ToString(),
"",
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
private void GetPoints(System.Text.StringBuilder sb, EnvDTE.CodeElement codeElement, EnvDTE.vsCMPart part)
{
GetStartPoint(sb, codeElement, part);
GetEndPoint(sb, codeElement, part);
sb.AppendLine();
}
private void GetStartPoint(System.Text.StringBuilder sb, EnvDTE.CodeElement codeElement, EnvDTE.vsCMPart part)
{
EnvDTE.TextPoint textPoint;
sb.Append(part.ToString() + " Start: ");
try
{
textPoint = codeElement.GetStartPoint(part);
sb.AppendLine(textPoint.Line.ToString() + ", " + textPoint.DisplayColumn.ToString());
}
catch (Exception ex)
{
sb.AppendLine(ex.GetType().Name);
}
}
private void GetEndPoint(System.Text.StringBuilder sb, EnvDTE.CodeElement codeElement, EnvDTE.vsCMPart part)
{
EnvDTE.TextPoint textPoint;
sb.Append(part.ToString() + " End: ");
try
{
textPoint = codeElement.GetEndPoint(part);
sb.AppendLine(textPoint.Line.ToString() + ", " + textPoint.DisplayColumn.ToString());
}
catch (Exception ex)
{
sb.AppendLine(ex.GetType().Name);
}
}
namespace ClassLibrary1
{
public class Class1
{
public int f1()
{
return 0;
}
public int p1
{
get
{
return 0;
}
}
public int f2() => 0;
public int p2 => 0;
}
}
Version Used: Visual Studio 2017 15.2 (26430.16 Release)
Steps to Reproduce:
Expected Behavior: it is already bad that so many points throw NotImplementedException, but for expression-bodied members a COMException is returned when the part is vsCMPartBody.
Actual Behavior: at the very least, NotImplementedException should be returned instead of COMException. And even better, return actual information since there is a body and for regular members the body points are returned.