我希望以编程方式获取WinMD文件中的所有命名空间。我更喜欢基于PowerShell或C#的解决方案,因为我需要它在脚本中,但只要完成工作,任何语言都会这样做。
以下是我现在使用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 up the handlers
domain.ReflectionOnlyAssemblyResolve += assemblyHandler;
WindowsRuntimeMetadata.ReflectionOnlyNamespaceResolve += namespaceHandler;
// Load it again! (WinMD components)
return Assembly.ReflectionOnlyLoadFrom(path);
}
finally
{
// Detach the handlers
domain.ReflectionOnlyAssemblyResolve -= assemblyHandler;
WindowsRuntimeMetadata.ReflectionOnlyNamespaceResolve -= namespaceHandler;
}
}
出于某种原因,它似乎并没有起作用。当我运行它时,当我尝试加载WinMD文件时,我得到ReflectionTypeLoadException
。 (有关详细信息,请参阅this question。)
所以我的问题是,如果Reflection API不起作用,最好的方法是什么?当您在WinRT类型上按F12时,Visual Studio或ILSpy等工具如何做到这一点?有没有办法从PowerShell做到这一点?
TL; DR:如何从WinMD文件中提取所有命名空间?接受任何语言解决方案。
感谢。
答案 0 :(得分:0)
结束@ PetSerAl建议使用 Mono.Cecil ,这实际上非常可靠。这是我最终采用的方法(用PowerShell编写):
# Where the 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 for 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)
{
# Already downloaded it from a previous script run/function call
Add-Type -Path $assemblyPath
return
}
ri -Recurse -Force $directory 2>&1 | Out-Null
mkdir -f $directory | Out-Null # prevent this from being interpreted as a return value
iwr $url -OutFile $nupkg
Extract-Nupkg $nupkg -Out $directory
Add-Type -Path $assemblyPath
}
您可以找到脚本here.
的完整内容