4

I've got a Package for Vs2010 that currently follows

EnvDTE=>Solution=>Projects=>CodeModel=>CodeElements

to do the following recursively and find classes

var q = elements.Cast<CodeElement>()
            .Where(x => x is CodeClass || x is CodeNamespace)
            .Where(x => x.Name.StartsWith("System") == false)
            .Where(x=>x.Name.StartsWith("Infragistics")==false)
            .Where(x=>x.Name.StartsWith("Microsoft")==false)
            .Where(x => x.Name.StartsWith("ICSharpCode")==false);

It runs fairly slowly, is there a way to restrict this query/search to only classes/types defined within the current project?

As I understand it FileCodeModel is neither useful nor appropriate since that would require opening every project Item.

Maslow
  • 18,464
  • 20
  • 106
  • 193

2 Answers2

3

The way that I use to navigate the code elements of a Project.CodeModel or ProjectItem.FileCodeModel is described in the article:

HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in http://www.mztools.com/articles/2006/MZ2006008.aspx

If performance is an issue, try if avoiding the LINQ layer enhances the performance. Other than that there is no much to do since the CodeElements collections returned by EnvDTE return all the code elements and it is afterwards when you filter.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • Just note with this, it doesn't restrict itself to classes within the project - it grabs them if they're referenced anywhere (resulting with a good deal of the BCL in there). I'm only just looking at this now so I don't have an alternate answer! – George R Jun 03 '12 at 05:44
2

Old question, but I will post this for future reference.

There is a InfoLocation property on the CodeElement class that is equal to vsCMInfoLocation.vsCMInfoLocationExternal when an element is coming from a referenced assembly.

For more information: http://msdn.microsoft.com/en-us/library/envdte.codeelement.infolocation.aspx

Jason
  • 4,557
  • 5
  • 31
  • 40
  • In the context of a VS package, getting the selected project and trying to enumerate through the classes, it seems everything is returning `vsCMInfoLocationExternal` meaning I can tell the difference between stuff defined in the project I'm looking at and BCL stuff – Matt Burland Jul 21 '16 at 19:52
  • 1
    @MattBurland I find that while namespaces, even those defined only in my project, are marked as External, classes and other types are marked as `vsCMInfoLocationProject` correctly. – Dai Sep 06 '16 at 10:45