从 .Net 4.x 应用程序反映 WinRT 可执行文件

编程入门 行业动态 更新时间:2024-10-10 19:27:10
本文介绍了从 .Net 4.x 应用程序反映 WinRT 可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在控制台应用程序中;如果我执行:

In a console application; If I execute:

Assembly.LoadFrom(@"c:...MyWinRTApp.exe")

我明白了:

System.BadImageFormatException occurred HResult=-2147024885 Message=Could not load file or assembly 'file:///C:\_...MyWinRTApp.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format. Source=mscorlib

有什么办法可以解决这个问题吗?

Is there any way around this?

关于下面维亚切斯拉夫沃尔科夫"的回答,我现在更进一步,谢谢.但是我现在遇到了一个不同的问题.

In relation to "Vyacheslav Volkov"'s answer below, I now get a step further, thank you. However I now get a different issue.

"assembly.GetExportedTypes()" now throws "Cannot resolve dependency to Windows Runtime type 'Windows.UI.Xaml.Application'. When using the ReflectionOnly APIs, dependent Windows Runtime assemblies must be resolved on demand through the ReflectionOnlyNamespaceResolve event."

如果我尝试 ReflectionOnlyLoad 引用的程序集,则会出现错误:

If I try to ReflectionOnlyLoad the referenced assemblies, then I get the error:

"Could not load file or assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)".

这与加载 winmd 引用有关,并在此处的帖子中进行了解释:无法加载文件或程序集Windows,版本=255.255.255.255,Culture=neutral,PublicKeyToken=null,ContentType=WindowsRuntime".

This is related to loading winmd references, and explained in the post here: Could not load file or assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.

我正在尝试的完整代码是这样的:

The full code I'm trying is this:

using System.Runtime.InteropServices.WindowsRuntime; var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath); /*WindowsRuntimeMetadata.ReflectionOnlyNamespaceResolve += (x, y) => { y.NamespaceName ??? y.ResolvedAssemblies.Add(Assembly.ReflectionOnlyLoadFrom(???)); return; };*/ foreach (var references in assembly.GetReferencedAssemblies()) { try { Assembly.ReflectionOnlyLoad(references.FullName); } catch (FileNotFoundException) { var fi = new FileInfo(assemblyPath); var fi2Name = String.Format("{0}\{1}.dll", fi.DirectoryName, references.Name); var fi2 = new FileInfo(fi2Name); if (fi2.Exists) { Assembly.ReflectionOnlyLoadFrom(fi2.FullName); } } catch (FileLoadException) { // When a winmd assembly is attempted. } } return assembly;

还有什么想法吗?

谢谢乔恩

最新思路成功解析{Windows.UI.Xaml, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime}".

The latest idea successfully resolves "{Windows.UI.Xaml, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime}".

但是,当在Client.exe"程序集上调用.GetExportedTypes()"时,只会为命名空间Windows.UI.Xaml"触发一次ReflectionOnlyNamespaceResolve"事件,该事件解析为C:windowssystem32WinMetadataWindows.UI.Xaml.winmd".

However, when ".GetExportedTypes()" is called on the "Client.exe" assembly, the 'ReflectionOnlyNamespaceResolve' event is only fired once for namespace "Windows.UI.Xaml", which resolves to "C:windowssystem32WinMetadataWindows.UI.Xaml.winmd".

然后在.GetExportedTypes()"中抛出异常,即无法解析对程序集'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 的依赖,因为它尚未预加载.使用 ReflectionOnly API 时,必须通过 ReflectionOnlyAssemblyResolve 事件预先加载或按需加载依赖程序集.".

An exception is then thrown within ".GetExportedTypes()", which is "Cannot resolve dependency to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.".

推荐答案

如果您只想发现包含的类型,您应该使用 Assembly.ReflectionOnlyLoad 方法.

If you want only to discover the containing types you should use the Assembly.ReflectionOnlyLoad method.

Assembly.ReflectionOnlyLoadFrom(@"c:...MyWinRTApp.exe")

更新

这是对我有用的代码:

AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (sender, eventArgs) => Assembly.ReflectionOnlyLoad(eventArgs.Name); WindowsRuntimeMetadata.ReflectionOnlyNamespaceResolve += (sender, eventArgs) => { string path = WindowsRuntimeMetadata.ResolveNamespace(eventArgs.NamespaceName, Enumerable.Empty<string>()) .FirstOrDefault(); if (path == null) return; eventArgs.ResolvedAssemblies.Add(Assembly.ReflectionOnlyLoadFrom(path)); }; Assembly loadFrom = Assembly.ReflectionOnlyLoadFrom(@"C:....WinRTApp.exe"); Type[] types = loadFrom.GetExportedTypes(); foreach (Type type in types) { Console.WriteLine(type); }

更多推荐

从 .Net 4.x 应用程序反映 WinRT 可执行文件

本文发布于:2023-11-13 06:05:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   可执行文件   Net   WinRT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!