将C#reflection代码移植到Metro-Ui
我正在尝试移植使用reflection的现有C#类(通用工厂),但我无法编译这段代码:
Type[] types = Assembly.GetAssembly(typeof(TProduct)).GetTypes(); foreach (Type type in types) { if (!typeof(TProduct).IsAssignableFrom(type) || type == typeof(TProduct)) ...
我试着查看.NET Framework for Windows Metro Style Apps和Assembly Class中的Reflection ,在那里我找到了一个因为“使用System.Security.Permissions”而无法编译的示例。
就像您链接的第一页所说的那样,您需要使用TypeInfo
而不是Type
。 还有其他更改,例如, Assembly
具有DefinedTypes
属性而不是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) { } }