从VS2010扩展中获取当前解决方案中的程序集的类型信息

时间:2011-07-08 17:07:23

标签: visual-studio visual-studio-2010 vsx envdte

我正在为VS2010中的UML序列图构建一个命令扩展,并且需要一个在当前解决方案中实现特定接口的类型列表。如何从扩展程序访问类型和程序集信息?到目前为止,我的所有尝试都刚刚列出了在原始扩展项目中加载的程序集,而不是VS当前正在编辑的程序集。

1 个答案:

答案 0 :(得分:5)

这是我最终得到的解决方案,使用linq简化搜索:

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;

此块搜索当前打开的解决方案中包含的所有项目,获取所有引用,加载程序集,并搜索它们以实现接口的类型。