将C#反射代码移植到Metro-Ui [英] Porting C# reflection code to Metro-Ui

查看:58
本文介绍了将C#反射代码移植到Metro-Ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

我尝试查看组装类,在这里我发现了一个由于使用System.Security.Permissions"而无法编译的示例.

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".

推荐答案

就像您链接到的第一页所说,您需要使用TypeInfo而不是Type.还有其他更改,例如,Assembly具有DefinedTypes属性而不是GetTypes()方法.修改后的代码如下所示:

Just like the first page you linked to says, you need to use TypeInfo instead of Type. There are also other changes, for example, Assembly has a DefinedTypes property instead of GetTypes() method. 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)
    { }
}

这篇关于将C#反射代码移植到Metro-Ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆