简体   繁体   English

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

[英]Porting C# reflection code to Metro-Ui

I'm trying to port an existing C# class (a generic factory) that uses reflection, but I can't get this piece of code to compile: 我正在尝试移植使用反射的现有C#类(通用工厂),但我无法编译这段代码:

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

I tried looking at the Reflection in the .NET Framework for Windows Metro Style Apps and Assembly Class , where I found an example that didn't compile because of the "using System.Security.Permissions". 我试着查看.NET Framework for Windows Metro Style AppsAssembly Class中的Reflection ,在那里我发现了一个因为“使用System.Security.Permissions”而无法编译的示例。

Just like the first page you linked to says, you need to use TypeInfo instead of Type . 就像您链接到的第一页所说的那样,您需要使用TypeInfo而不是Type There are also other changes, for example, Assembly has a DefinedTypes property instead of GetTypes() method. 还有其他更改,例如, Assembly具有DefinedTypes属性而不是DefinedTypes GetTypes()方法。 The modified code could look like this: 修改后的代码可能如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM