簡體   English   中英

如何使這個反射與WinRT一起工作

[英]How to make this Reflection work with WinRT

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 <>中產生任何結果

嘗試ONE失敗

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

試錯導致了這一點。 我希望它更清楚:(

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

嘗試這個:

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

有一個名為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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM