Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使此反射与WinRT一起工作_C#_Reflection - Fatal编程技术网

C# 如何使此反射与WinRT一起工作

C# 如何使此反射与WinRT一起工作,c#,reflection,C#,Reflection,Windows应用商店/WinRT应用程序的反射与我以前的不同。如何重构此代码块以使用WinRT 注意:此代码块在不针对Windows应用商店应用程序的PCL中工作。(第104页)。一旦我将目标更改为Profile158,它将不再编译 实际上,我们有一个名为imiglation的接口。然后我们有一个继承IMigration的抽象类(迁移)。在此基础上,我们创建了如下迁移 public class Migration001 : Migration{ } public class Migrati

Windows应用商店/WinRT应用程序的反射与我以前的不同。如何重构此代码块以使用WinRT

注意:此代码块在不针对Windows应用商店应用程序的PCL中工作。(第104页)。一旦我将目标更改为Profile158,它将不再编译

实际上,我们有一个名为
imiglation
的接口。然后我们有一个继承IMigration的抽象类(
迁移
)。在此基础上,我们创建了如下迁移

public class Migration001 : Migration{

}

public class Migration002 : Migration{

}

// and so on.
我正在苦苦挣扎的代码需要做的是将所有迁移提取到一个IEnumerable中,这样我就可以循环它们并按顺序运行它们。正如我之前所说的,第一个代码块在不以WinRT为目标时可以工作,但现在它需要这样做,因为

// Cannot resolve symbol 'Assembly'
// Cannot resolve symbol 'IsAssignableFrom'
我尝试了两种不同的方法,但在IEnumberable中都没有结果

尝试一次失败

var migrations =
    typeof(IMigration).GetTypeInfo().Assembly.DefinedTypes
                          .Where(type => type.IsSubclassOf(typeof(IMigration)) && !type.IsAbstract)
                          .OrderBy(type => type.Name);

// enumeration yielded no results
第二次尝试失败

var migrationInterfaceType = typeof (IMigration);
var migrations = migrationInterfaceType.GetTypeInfo().Assembly.DefinedTypes
                          .Where(type => migrationInterfaceType.GetTypeInfo().IsSubclassOf(type.GetType()) && !type.IsAbstract)
                          .OrderBy(type => type.Name);

// enumeration yielded no results
万一你想知道,我是如何运行迁移的。。。没什么特别的

foreach (var migration in migrations)
{
    Run(_serviceLocator.GetInstance<IMigration>(migration.Name));
}
foreach(迁移中的var迁移)
{
运行(_serviceLocator.GetInstance(migration.Name));
}
试试这个:

var migrations = typeof(IMigration).GetTypeInfo()
                                   .Assembly
                                   .DefinedTypes
                                   .Where(type => type.GetInterfaces()
                                                      .Any(itf=> 
                                         itf == typeof(IMigration)) && 
                                         !type.IsAbstract)
                                   .OrderBy(type => type.Name);

试错导致了这一点。我真希望它能更清楚一点:(


有一个名为GetTypeInfo的扩展方法可以执行此操作:

using System.Reflection;

.....

var migrationInterfaceType = typeof (Migration);
var migrations =
    migrationInterfaceType.GetTypeInfo().Assembly.ExportedTypes
                          .Where(type => migrationInterfaceType.GetTypeInfo().IsSubclassOf(type) && !type.IsAbstract)
                          .OrderBy(type => type.Name);

失败尝试的结果是什么?你期望什么?你做了什么?你第一次评论中的错误(你真的应该把问题弄清楚)答案是,第二个可能会消失。是的,我一直在研究。结果是一个空的
IEnumerable
。当不需要针对Windows应用商店应用程序时,第一个代码块可以工作。问题是你没有解释该代码应该做什么。你只在注释中输入两个编译器错误,然后说出来“不能与WinRT正常工作”。您应该知道这不是一个准确的问题描述。我想现在“尝试失败”的意思是“返回一个空的
IEnumerable
",但这也不能告诉我们您希望它返回什么。如果您能提供更多关于您正在尝试执行的操作的信息,这将非常有助于您的问题。我知道足够多的反射,可以进行猜测,因此我发现了。我们有以下类。
public class Migration001:Migration
,以及抽象类
public abstract类迁移:imiglation
。我希望它返回一个
IEnumerable
。不幸的是
GetInterfaces
不能与WinRT一起工作。它需要与
IsSubclassOf
var currentAssembly = GetType().GetTypeInfo().Assembly;
var migrations = currentAssembly.DefinedTypes
                                .Where( type => type.ImplementedInterfaces
                                                    .Any(inter => inter == typeof (IMigration)) && !type.IsAbstract )
                                .OrderBy( type => type.Name );
using System.Reflection;

.....

var migrationInterfaceType = typeof (Migration);
var migrations =
    migrationInterfaceType.GetTypeInfo().Assembly.ExportedTypes
                          .Where(type => migrationInterfaceType.GetTypeInfo().IsSubclassOf(type) && !type.IsAbstract)
                          .OrderBy(type => type.Name);