c# - Reflecting a WinRT executable from a .Net 4.x App -
in console application; if execute:
assembly.loadfrom(@"c:\...\mywinrtapp.exe")
i get:
system.badimageformatexception occurred hresult=-2147024885 message=could not load file or assembly 'file:///c:\_...\mywinrtapp.exe' or 1 of dependencies. attempt made load program incorrect format. source=mscorlib
is there way around this?
edit 1
in relation "vyacheslav volkov"'s answer below, step further, thank you. different issue.
"assembly.getexportedtypes()" throws "cannot resolve dependency windows runtime type 'windows.ui.xaml.application'. when using reflectiononly apis, dependent windows runtime assemblies must resolved on demand through reflectiononlynamespaceresolve event."
if try reflectiononlyload referenced assemblies, error:
"could not load file or assembly 'windows, version=255.255.255.255, culture=neutral, publickeytoken=null, contenttype=windowsruntime' or 1 of dependencies. operation not supported. (exception hresult: 0x80131515)".
this related loading winmd references, , explained in 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 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 winmd assembly attempted. } } return assembly;
any more ideas?
thanks, jon
edit 2
the latest idea resolves "{windows.ui.xaml, version=255.255.255.255, culture=neutral, publickeytoken=null, contenttype=windowsruntime}".
however, when ".getexportedtypes()" called on "client.exe" assembly, 'reflectiononlynamespaceresolve' event fired once namespace "windows.ui.xaml", resolves "c:\windows\system32\winmetadata\windows.ui.xaml.winmd".
an exception thrown within ".getexportedtypes()", "cannot resolve dependency assembly 'system.runtime, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a' because has not been preloaded. when using reflectiononly apis, dependent assemblies must pre-loaded or loaded on demand through reflectiononlyassemblyresolve event.".
if want discover containing types should use assembly.reflectiononlyload method.
assembly.reflectiononlyloadfrom(@"c:\...\mywinrtapp.exe")
update
here's code works me:
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); }
Comments
Post a Comment