Porting C# reflection code to Metro-Ui

醉酒当歌 提交于 2019-12-10 18:58:25

问题


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

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


回答1:


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


来源:https://stackoverflow.com/questions/9680757/porting-c-sharp-reflection-code-to-metro-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!