Reputation: 841
I'm building a command extension for the UML Sequence Diagram in VS2010, and need a list of types that implement a particular interface in the current solution. How do you access type and assembly information from an extension? So far all of my attempts have just listed assemblies loaded in the original extension project, not the one that VS is currently editing.
Upvotes: 3
Views: 958
Reputation: 841
Here's the solution I finally arrived at, using linq to simplify the search:
DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
var types = from Project project in dte.Solution.Projects
from Reference reference in (References)project.Object.References
where reference.Type == prjReferenceType.prjReferenceTypeAssembly
from t in Assembly.LoadFile(reference.Path).GetTypes()
where t != typeof(IInterface) && typeof(IInterface).IsAssignableFrom(t)
select t;
This block searches through all projects contained in the currently open solution, gets all of their references, loads the ones which are assemblies, and searches them for types that implement the interface.
Upvotes: 5