我有以下代码在PCL中运行正常,其中TargetFrameworkVersion为4.0,TargetFrameworkProfile为Profile104。
public class AppCapabilitiesRepository : IAppCapabilityRepository
{
private readonly Type _typeOfAppCapability = typeof (IAppCapability);
public IList<IAppCapability> GetCapabilities()
{
var capabilities = Assembly.GetExecutingAssembly().GetTypes().Where(IsAppCapability).ToArray();
var viewModels = capabilities.Select(capability => ((IAppCapability)Activator.CreateInstance(capability)))
.Where(c => c.IsActive)
.OrderBy(c => c.Popularity).ToList();
return viewModels;
}
private bool IsAppCapability(Type type)
{
return _typeOfAppCapability.IsAssignableFrom(type) && !type.IsAbstract && !type.IsInterface;
}
}
将项目升级到4.5的TargetFrameworkVersion和Profile259的TargetFrameworkProfile后,无法再找到这些反射API:
Assembly.GetExecutingAssembly,Type.IsAbstract和Type.IsInterface。
如果这很重要,这个解决方案也使用MvvmCross 3.5.1。
我现在该怎么办?
答案 0 :(得分:2)
感谢Stuart提示。我需要做的API更改如下:
Assembly.GetExecutingAssembly => this.GetType().GetTypeInfo().Assembly
Type.IsAbstract => TypeInfo.IsAbstract where TypeInfo.GetTypeInfo()
Type.IsInterface => TypeInfo.IsInterface where TypeInfo = Type.GetTypeInfo()
修改后的代码如下:
public class AppCapabilitiesRepository : IAppCapabilityRepository
{
private readonly Type _typeOfAppCapability = typeof (IAppCapability);
public IList<IAppCapability> GetCapabilities()
{
var capabilities = GetType().GetTypeInfo().Assembly.GetTypes().Where(IsAppCapability).ToArray();
var viewModels = capabilities.Select(capability => ((IAppCapability)Activator.CreateInstance(capability)))
.Where(c => c.IsActive)
.OrderBy(c => c.Popularity).ToList();
return viewModels;
}
private bool IsAppCapability(Type type)
{
var typeInfo = type.GetTypeInfo();
return _typeOfAppCapability.IsAssignableFrom(type) && !typeInfo.IsAbstract && !typeInfo.IsInterface;
}
}
有些法律在这里被违反,但是......
希望这有助于其他人。