c# - What's the best way to get the namespaces in a WinMD file? -
i'm looking of namespaces in winmd file programmatically. prefer powershell or c#-based solution since need in script, language long gets job done.
here code have right now, using assembly.reflectiononlyloadfrom
:
var domain = appdomain.currentdomain; resolveeventhandler assemblyhandler = (o, e) => assembly.reflectiononlyload(e.name); eventhandler<namespaceresolveeventargs> namespacehandler = (o, e) => { string file = windowsruntimemetadata .resolvenamespace(e.namespacename, array.empty<string>()) .firstordefault(); if (file == null) return; var assembly = assembly.reflectiononlyloadfrom(file); e.resolvedassemblies.add(assembly); }; try { // load it! (plain .net assemblies) return assembly.loadfrom(path); } catch { try { // hook handlers domain.reflectiononlyassemblyresolve += assemblyhandler; windowsruntimemetadata.reflectiononlynamespaceresolve += namespacehandler; // load again! (winmd components) return assembly.reflectiononlyloadfrom(path); } { // detach handlers domain.reflectiononlyassemblyresolve -= assemblyhandler; windowsruntimemetadata.reflectiononlynamespaceresolve -= namespacehandler; } }
for reason, doesn't seem working. when run it, i'm getting reflectiontypeloadexception
when try load winmd files. (you can see this question full details.)
so question is, what's best way go doing this, if reflection apis aren't working? how tools visual studio or ilspy when hit f12 on winrt type? there way powershell?
tl;dr: how extract of namespaces winmd file? language solution accepted.
thanks.
ended taking @petseral's suggestion using mono.cecil, pretty solid. here's approach ended taking (written in powershell):
# real work happens function get-namespaces($assembly) { add-cecilreference $moduledefinition = [mono.cecil.moduledefinition] $module = $moduledefinition::readmodule($assembly) return $module.types | ? ispublic | % namespace | select -unique } function extract-nupkg($nupkg, $out) { add-type -assemblyname 'system.io.compression.filesystem' # powershell lacks native support zip $zipfile = [io.compression.zipfile] $zipfile::extracttodirectory($nupkg, $out) } function add-cecilreference { $url = 'https://www.nuget.org/api/v2/package/mono.cecil' $directory = $psscriptroot, 'bin', 'mono.cecil' -join '\' $nupkg = join-path $directory 'mono.cecil.nupkg' $assemblypath = $directory, 'lib', 'net45', 'mono.cecil.dll' -join '\' if (test-path $assemblypath) { # downloaded previous script run/function call add-type -path $assemblypath return } ri -recurse -force $directory 2>&1 | out-null mkdir -f $directory | out-null # prevent being interpreted return value iwr $url -outfile $nupkg extract-nupkg $nupkg -out $directory add-type -path $assemblypath }
you can find full contents of script here.
Comments
Post a Comment