如何使用Visual Studio Extension从当前解决方案中收集类型?

时间:2013-10-02 17:01:13

标签: c# visual-studio-2012 visual-studio-extensions vsix vspackage

我创建了Visual Studio 2012 Package(使用VS2012 SDK)。此扩展(如果安装在客户端的IDE环境中)应具有从开发人员正在处理的当前打开的解决方案中收集所有特定类型的功能。 Visual Studio Designer for ASP.NET MVC Application Project中嵌入了类似的功能,其中开发人员实现模型/控制器类,构建项目,然后能够在Scaffolding UI(Designer的下拉列表)中访问此类型。相应的功能也可以在WPF,WinForms视觉设计师等中使用。

假设我的扩展程序必须从当前解决方案中收集所有类型,这些解决方案实现ISerializable接口。步骤如下:开发人员创建特定的类,重建包含项目/解决方案,然后执行扩展UI提供的某些操作,从而涉及执行ISerializable类型收集。

我尝试使用反射实现收集操作:

List<Type> types = AppDomain.CurrentDomain.GetAssemblies().ToList()
                  .SelectMany(s => s.GetTypes())
                  .Where(p => typeof(ISerializable).IsAssignableFrom(p) && !p.IsAbstract).ToList();

但是上面的代码会引发System.Reflection.ReflectionTypeLoadException异常:

System.Reflection.ReflectionTypeLoadException was unhandled by user code
  HResult=-2146232830
  Message=Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
  Source=mscorlib
  StackTrace:
       at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
       at System.Reflection.RuntimeModule.GetTypes()
       at System.Reflection.Assembly.GetTypes()
(...)  
LoaderException: [System.Exception{System.TypeLoadException}]
{"Could not find Windows Runtime type   'Windows.System.ProcessorArchitecture'.":"Windows.System.ProcessorArchitecture"}
(...)

如何正确实施从当前构建的解决方案中收集特定类型的操作?

2 个答案:

答案 0 :(得分:1)

我试图做类似的事情,不幸的是,到目前为止我发现的唯一方法就是通过以下方式(我感觉有点乱,但也许对某些具体情况进行一些调整可能没问题) )

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
IEnumerable<Type> types = assemblies.SelectMany(x => GetLoadableTypes(x));

...

public static IEnumerable<Type> GetLoadableTypes(Assembly assembly)
{
    try
    {
        return assembly.GetTypes();
    }
    catch (ReflectionTypeLoadException e)
    {
        return e.Types.Where(t => t != null);
    }
}

这会为您提供所有类型,但您可以过滤掉您想要的任何内容。

参考这篇文章:How to prevent ReflectionTypeLoadException when calling Assembly.GetTypes()

答案 1 :(得分:0)

我不确定我是否理解正确,但如果我这样做就会成功:

var assembly = Assembly.GetExecutingAssembly();
IEnumerable<Type> types = 
      assembly.DefinedTypes.Where(t => IsImplementingIDisposable(t))
                           .Select(t => t.UnderlyingSystemType);

........

private static bool IsImplementingIDisposable(TypeInfo t)
{
     return typeof(IDisposable).IsAssignableFrom(t.UnderlyingSystemType);
}