如何使用Visual Studio扩展,以收集当前的解决方案类型? [英] How to collect types from current solution using Visual Studio Extension?

查看:473
本文介绍了如何使用Visual Studio扩展,以收集当前的解决方案类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的Visual Studio 2012包(使用VS2012 SDK)。这个扩展(如果安装在客户端的IDE环境)应具有,除其他事项外,收集从当前打开的溶液显影剂正在所有特定类型的功能性。类似的功能嵌入在Visual Studio设计的ASP.NET MVC应用程序项目,其中开发者实现一个型号/ Controller类,建立一个项目,然后是能够脚手架UI(Designer的下拉列表)来访问此类型。相应的功能也可在WPF中,的WinForms可视化设计等。

I have created Visual Studio 2012 Package (using VS2012 SDK). This Extension (if installed on the client's IDE environment) should has, among other things, a functionality of collecting all specific types from currently opened solution which developer is working on. A similar feature is embedded in Visual Studio Designer for ASP.NET MVC Application Project, where developer implements a Model/Controller class, build a project, and then is able to access this type in Scaffolding UI (Designer's dropdown list). The corresponding features are also available in WPF, WinForms Visual Designers, etc.

让我们说,我的分机有收集来自当前解决方案的所有类型,实施 ISerializable的接口。这些步骤如下:开发者创建特定类,重建包含项目/解决方案,然后做推而广之的UI提供了一些动作,这样就涉及执行 ISerializable的类型收集

Let's say that my extension has to collect all types from current solution, which implement ISerializable interface. The steps are following: Developer creates specific class, rebuild containing project/solution, then do some action provided by extension UI, thus involves performing ISerializabletypes collecting.

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

I have tried to implement collecting operation using reflection:

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"}
(...)

我怎样才能正确地实现从目前内置的解决方案收集特定类型的操作?

How can I properly implement operation of collecting specific types from currently built solution?

推荐答案

我试图做类似的事情,不幸的是解决这个问题的唯一办法,到目前为止,我发现是通过执行以下(这是我的感觉是有点凌乱,但也许有一些调整的具体情况可能是好的)

I was trying to do a similar thing and unfortunately the only way around this I've found so far is by doing the following (which I feel is a bit messy, but maybe with some tweaking for a specific situation might be ok)

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);
    }
}

这会给你的所有类型,但是你可以过滤掉你希望的任何

This would give you all types, but you can filter out whatever you wish.

引用这篇文章:的如何防止reflectionTypeLoadException调用Assembly.GetTypes()时

这篇关于如何使用Visual Studio扩展,以收集当前的解决方案类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆