【问题标题】:Get all classes in a referenced assembly for Windows 8.1 Universal app获取 Windows 8.1 通用应用程序引用程序集中的所有类
【发布时间】:2014-11-07 12:49:01
【问题描述】:

我有一个项目要从 Windows Phone 8 迁移到通用 Windows 8.1。

场景

在解决方案中,我有多个程序集实现了在通用程序集中定义的接口(但并非全部在每个程序集中,它们各不相同),但所有程序集都实现或扩展了接口IResolver

我在“主”项目/程序集中使用反射来注册引用程序集中的类,方法是向其传递该程序集的 IResolver(接口)的实现实例。

代码

using System.Reflection;
...
public void Register(IResolver resolver)
{
   foreach (Type classType in resolver.GetType().Assembly.GetTypes())
   {
      // Register the class type with the ioc container
   }
}

它就像一个魅力:-)

问题

System.Type 不包含Assembly 的定义,并且找不到接受System.Type 类型的第一个参数的扩展方法Assembly

Type.Assembly 的文档中指出:

支持:Windows Phone 8.1、Windows Phone Silverlight 8.1、Windows Phone Silverlight 8

因此不支持在 Windows 8.1 中,但即使在 Windows Phone 8.1 项目中,仍然表示不包含 Assembly 的定义。

如果这还不够,Assemly 类没有实现方法GetTypes()

问题

如何从接口的实例中获取 Universal App 的引用程序集中定义的所有类?

有什么建议或替代方案吗?

不起作用的事情

Assembly.GetExecutingAssembly()

错误:System.Reflection.Assembly 不包含GetExecutingAssembly 的定义

【问题讨论】:

标签: c# .net windows-8.1 windows-phone-8.1 .net-assembly


【解决方案1】:

嗯,这很烦人......

GetType().GetTypeInfo()

public void Register(IResolver resolver)
{
   foreach (Type classType in resolver.GetType().GetTypeInfo().Assembly.GetExportedTypes())
   {
      // Register the class type with the ioc container
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多