sgn*_*gon 6 c# vspackage vsix visual-studio-extensions visual-studio-2012
我创建了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();
Run Code Online (Sandbox Code Playgroud)
但是上面的代码会导致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"}
(...)
Run Code Online (Sandbox Code Playgroud)
如何正确实施从当前构建的解决方案中收集特定类型的操作?
我不确定我是否正确理解了你的意思,但如果我是这样的话,就可以了:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1476 次 |
| 最近记录: |