如何使这个反射与WinRT一起工作

时间:2013-11-15 18:35:47

标签: c# reflection

Windows Store / WinRT应用程序的反映与我以前的不同。如何重构此代码块以使用WinRT?

注意:此代码块在PCL中工作,不以Windows应用商店应用为目标。 (Profile104)。只要我将目标更改为Profile158,它就不会再编译。

var migrationInterfaceType = typeof (IMigration);
var migrations =
    migrationInterfaceType.Assembly.GetTypes()
                          .Where(type => migrationInterfaceType.IsAssignableFrom(type) && !type.IsAbstract)
                          .OrderBy(type => type.Name);

// Cannot resolve symbol 'Assembly'
// Cannot resolve symbol 'IsAssignableFrom'

基本上我们有一个名为IMigration的界面。然后我们有一个继承IMigration的抽象类(Migration)。从那里我们创建我们的迁移如下。

public class Migration001 : Migration{

}

public class Migration002 : Migration{

}

// and so on.

我正在努力解决的代码是将所有迁移提取到IEnumerable<>这样我就可以遍历它们并按顺序运行它们。正如我之前所说,第一个代码块在没有针对WinRT的情况下工作,但现在它需要,它将无法编译,因为

// Cannot resolve symbol 'Assembly'
// Cannot resolve symbol 'IsAssignableFrom'

我尝试了两种不同的方法,但两者都没有在IEnumberable<>

中产生任何结果

尝试失败

var migrations =
    typeof(IMigration).GetTypeInfo().Assembly.DefinedTypes
                          .Where(type => type.IsSubclassOf(typeof(IMigration)) && !type.IsAbstract)
                          .OrderBy(type => type.Name);

// enumeration yielded no results

尝试失败

var migrationInterfaceType = typeof (IMigration);
var migrations = migrationInterfaceType.GetTypeInfo().Assembly.DefinedTypes
                          .Where(type => migrationInterfaceType.GetTypeInfo().IsSubclassOf(type.GetType()) && !type.IsAbstract)
                          .OrderBy(type => type.Name);

// enumeration yielded no results

如果你想知道,这就是我如何运行迁移......没什么特别的。

foreach (var migration in migrations)
{
    Run(_serviceLocator.GetInstance<IMigration>(migration.Name));
}

3 个答案:

答案 0 :(得分:2)

试验和错误导致这一点。我希望它更清楚一点:(

var currentAssembly = GetType().GetTypeInfo().Assembly;
var migrations = currentAssembly.DefinedTypes
                                .Where( type => type.ImplementedInterfaces
                                                    .Any(inter => inter == typeof (IMigration)) && !type.IsAbstract )
                                .OrderBy( type => type.Name );

答案 1 :(得分:0)

试试这个:

var migrations = typeof(IMigration).GetTypeInfo()
                                   .Assembly
                                   .DefinedTypes
                                   .Where(type => type.GetInterfaces()
                                                      .Any(itf=> 
                                         itf == typeof(IMigration)) && 
                                         !type.IsAbstract)
                                   .OrderBy(type => type.Name);

答案 2 :(得分:0)

有一个名为GetTypeInfo的扩展方法可以做到这一点:

using System.Reflection;

.....

var migrationInterfaceType = typeof (Migration);
var migrations =
    migrationInterfaceType.GetTypeInfo().Assembly.ExportedTypes
                          .Where(type => migrationInterfaceType.GetTypeInfo().IsSubclassOf(type) && !type.IsAbstract)
                          .OrderBy(type => type.Name);