How to check if an ITypeMember/IParameter is "own code"

I want to check that a given ITypeMember or IParameter is part of a compiled project within the solution (i.e. no external (library) code).

My current two approaches are:

bool isOwnCode = element.GetSourceFiles().Count > 0;


bool isOwnCode = element.GetDeclarations().Count > 0;

a) Are they too restrictive/unrestrictive?
b) Is there a better alternative regarding performance/which version should be preferred?

Comment actions Permalink

The best way is to look at the type of the module where the element is declared. If it's an instance of IProjectPsiModule, it's come from a project in the current solution. If it's IAssemblyPsiModule, then it's from an assembly reference. There are several other types, such as IWebsitePsiModule, which is for website projects, and others for support of files that aren't directly included in a project (such as referenced JavaScript files that aren't part of a project).

So, all you need to do is:

var isFromSource = ITypeMember.Module is IProjectPsiModule;

Comment actions Permalink

Works great. Many thanks!


Please sign in to leave a comment.