c# - 从 .Net 4.x 应用程序反射(reflect) WinRT 可执行文件

标签 c# reflection windows-store-apps .net-assembly

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

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

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

编辑 1

关于下面“Vyacheslav Volkov”的回答,我现在更进一步了,谢谢。但是我现在遇到了一个不同的问题。

"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 引用的程序集,则会出现错误:

"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 引用有关,并在此处的帖子中进行了解释:Could not load file or assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' .

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

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;

还有什么想法吗?

谢谢,乔

编辑2

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

但是,当在“Client.exe”程序集上调用“.GetExportedTypes()”时,“ReflectionOnlyNamespaceResolve”事件仅针对命名空间“Windows.UI.Xaml”触发一次,解析为“C:\windows”\system32\WinMetadata\Windows.UI.Xaml.winmd”。

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

最佳答案

如果您只想发现包含的类型,您应该使用 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);
}

关于c# - 从 .Net 4.x 应用程序反射(reflect) WinRT 可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18101011/

相关文章:

c# - fiddler /C# : search content of request/response for special phrases

c# - 如何通过 C# 访问另一个程序集中的类?

reflection - 在不使用反射的情况下检查 Go 中的接口(interface)值是否为 nil

xaml - 当我更改主题设置时,为什么我的 AdaptiveTrigger 会触发?

c++ - 在 Metro C++ 中将图像从内容加载到 StorageFile^

c# - 为什么 C# 中的 Stack<T> 类允许 ElementAt(index) 而它是一个 ADT?

c# - 如何在 ms access 中以编程方式创建 GUID 自动编号

c# - SQL Server : "CREATE ASSEMBLY for assembly ' Test' failed because assembly 'Test' is malformed or not a pure . NET 程序集。”

java - 你如何让java方法注释在scala中工作

xaml - HeaderedItemsControl 的 Metro 等效项