将C#反射代码移植到Metro-Ui

时间:2012-03-13 08:47:59

标签: c# reflection windows-8 microsoft-metro windows-runtime

我正在尝试移植使用反射的现有C#类(通用工厂),但我无法编译这段代码:

Type[] types = Assembly.GetAssembly(typeof(TProduct)).GetTypes();
foreach (Type type in types)
{
    if (!typeof(TProduct).IsAssignableFrom(type) || type == typeof(TProduct))
...

我尝试查看Reflection in the .NET Framework for Windows Metro Style AppsAssembly Class,在那里我找到了一个因为“使用System.Security.Permissions”而无法编译的示例。

1 个答案:

答案 0 :(得分:6)

就像您关联的第一页所说的那样,您需要使用TypeInfo代替Type。还有其他更改,例如,Assembly具有DefinedTypes属性而非GetTypes()方法。修改后的代码可能如下所示:

var tProductType = typeof(TProduct).GetTypeInfo();
var types = tProductType.Assembly.DefinedTypes; // or .ExportedTypes
foreach (var type in types)
{
    if (!tProductType.IsAssignableFrom(type) || type == tProductType)
    { }
}
相关问题