我创建了一个Visual Studio 2012套件(使用VS2012 SDK)。如果将此扩展安装在客户端的IDE环境中,则除其他功能外,它还应该具有从开发人员正在工作的当前打开解决方案中收集所有特定类型的功能。类似的功能嵌入在ASP.NET MVC应用程序项目的Visual Studio设计器中,其中开发人员实现模型/控制器类,构建项目,然后能够在脚手架UI(设计器下拉列表)中访问此类型。相应的功能也适用于WPF、WinForms Visual Designers等。
假设我的扩展必须收集当前解决方案中实现ISerializable接口的所有类型。步骤如下:开发人员创建特定类,重建包含的项目/解决方案,然后执行扩展UI提供的某些操作,从而涉及执行ISerializable类型收集。
我尝试使用反射来实现收集操作:
但是上述代码会导致抛出 System.Reflection.ReflectionTypeLoadException 异常:
如何正确实现从当前构建的解决方案中收集特定类型的操作?
假设我的扩展必须收集当前解决方案中实现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"}
(...)
如何正确实现从当前构建的解决方案中收集特定类型的操作?