UWP反射Assembly.Load()无法在发布模式下使用.Net Native工具链

时间:2018-05-03 09:56:05

标签: c# reflection uwp system.reflection .net-native

我正在尝试使用一个PoC在运行时在UWP应用程序中动态加载程序集(UWP类库的DLL)。

目前UWP的限制,如果我将它们移动到 AppX 文件夹,我可以加载程序集。在调试模式下部署后,它与 Assembly.Load()方法配合使用。

但是当我尝试在发布模式下编译并运行应用程序并启用 .Net Native tool chain 选项时,它会引发运行时异常并且不会加载程序集。 / p>

异常

System.IO.FileNotFoundException: Cannot load assembly 'MyUIClassLibrary.UWP'. No metadata found for ths assembly.
System.Reflection.Runtime.Assemblies.RuntimeAssembly.GetRuntimeAssembly(RuntimeAssembly
+ 0x22
 at Assembly
System.Reflection.Runtime.General.ReflectionCoreCallbacksImplementation.Load(AssemblyName, Boolean) + 0x2eb827
 at System.Reflection.Assembly.Load(Reflection.AssemblyName assemblyRef)

代码示例

private UserControl LoadUserControl(string assemblyName)
{
    AssemblyName aName = new AssemblyName
    {
        Name = assemblyName,
        ProcessorArchitecture = ProcessorArchitecture.X86
    };

    var assembly = Assembly.Load(aName);

    Type[] tlist = assembly.GetTypes();

    return Activator.CreateInstance(tlist[0]) as UserControl;
}

...
...

//Loading UI assemblies at runtime
pivotItemOne.Content = LoadUserControl("MyUIClassLibrary.UWP");

我认为.Net Native工具链存在问题,并且在发布模式构建时避免了其他Dll。因为它在调试模式下工作正常。

如果我将UWP类库项目添加为我的主UWP应用程序的引用,那么程序集加载方法在Debug和Release配置中都能正常工作。

出于某种原因,我的真正目标是从AppX文件夹加载程序集而不在项目中引用它们。像在WPF应用程序中一样加载外部程序集。

请让我知道我错过了什么?或任何解决方法赞赏。提前谢谢。

注意:我的机器正在运行Windows 10 Fall Creator Update并使用VS2017

0 个答案:

没有答案
相关问题