【问题标题】:Get dll metadata in Linux environmentLinux环境下获取dll元数据
【发布时间】:2019-10-27 16:30:50
【问题描述】:

我希望能够在 Linux 环境中(即在运行任何 mcr.microsoft.com/dotnet/core/sdk docker 映像时)获取任何 dll 的元数据(尽可能支持 dotnet 版本)。

能做到吗?

我所说的元数据是指无需加载代码就可以了解的所有内容(最重要的 - 命名空间、版本)。

我观察到 Assembly.ReflectionOnlyLoadFrom(),但它似乎不支持 pre-dotnet-core 版本。

我不限于编程语言,只限于基于 Linux 的运行时。

【问题讨论】:

标签: c# .net dll .net-core .net-assembly


【解决方案1】:

使用 Microsoft.CodeAnalysis.Common 包中的 AssemblyMetadata,您可以读取 dll 的内容,例如版本、模块、类型、引用、属性以及几乎所有内容

var path = @"path/to/dll/file.dll";
var metadata = AssemblyMetadata.CreateFromFile(path);
var module = metadata.GetModules().First();
Console.WriteLine(module.Name);

var reader = module.GetMetadataReader();

var assemblyDef = reader.GetAssemblyDefinition();
Console.WriteLine(reader.GetString(assemblyDef.Name));
Console.WriteLine(assemblyDef.Version.ToString());

foreach (var typeDefHandle in reader.TypeDefinitions)
{
    var typeDef = reader.GetTypeDefinition(typeDefHandle);
    var fullName = (reader.GetString(typeDef.Namespace) + "::" + reader.GetString(typeDef.Name));
    Console.WriteLine(fullName);
}

【讨论】:

    【解决方案2】:

    我无法让任何标准工具在 Windows 之外工作(ilspy、dnspy、ildasm)

    然而,this question 中引用的 exiftool 似乎可以很好地提供一些元数据,尽管它仍然有限。

    exiftool Newtonsoft.Json.net45.dll 的示例输出:

    ExifTool Version Number         : 11.73
    File Name                       : Newtonsoft.Json.net45.dll
    Directory                       : .
    File Size                       : 660 kB
    File Modification Date/Time     : 2019:04:22 02:06:26+03:00
    File Access Date/Time           : 2019:10:28 10:11:25+02:00
    File Inode Change Date/Time     : 2019:10:28 10:09:56+02:00
    File Permissions                : rwxr-xr-x
    File Type                       : Win32 DLL
    File Type Extension             : dll
    MIME Type                       : application/octet-stream
    Machine Type                    : Intel 386 or later, and compatibles
    Time Stamp                      : 2092:04:05 06:43:32+03:00
    Image File Characteristics      : Executable, Large address aware, DLL
    PE Type                         : PE32
    Linker Version                  : 48.0
    Code Size                       : 665088
    Initialized Data Size           : 2048
    Uninitialized Data Size         : 0
    Entry Point                     : 0xa42ba
    OS Version                      : 4.0
    Image Version                   : 0.0
    Subsystem Version               : 6.0
    Subsystem                       : Windows command line
    File Version Number             : 12.0.2.23222
    Product Version Number          : 12.0.2.0
    File Flags Mask                 : 0x003f
    File Flags                      : (none)
    File OS                         : Win32
    Object File Type                : Dynamic link library
    File Subtype                    : 0
    Language Code                   : Neutral
    Character Set                   : Unicode
    Comments                        : Json.NET is a popular high-performance JSON framework for .NET
    Company Name                    : Newtonsoft
    File Description                : Json.NET
    File Version                    : 12.0.2.23222
    Internal Name                   : Newtonsoft.Json.dll
    Legal Copyright                 : Copyright © James Newton-King 2008
    Legal Trademarks                :
    Original File Name              : Newtonsoft.Json.dll
    Product Name                    : Json.NET
    Product Version                 : 12.0.2+4ab34b0461fb595805d092a46a58f35f66c84d6a
    Assembly Version                : 12.0.0.0
    

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 2016-07-03
      • 2013-05-17
      相关资源
      最近更新 更多