【问题标题】:Need a tool to dump the referenced types and members for an assembly需要一个工具来转储程序集的引用类型和成员
【发布时间】:2010-01-20 13:01:46
【问题描述】:

我需要一个可以将程序集使用的引用类型转储为机器可读格式的工具。

例如此代码在程序集“dummy.exe”中

static void Main()
{
  Console.WriteLine("Hello World");
}

会产生类似的东西

<references assembly="dummy.exe">
  <mscorlib>
    <System.Console>
      <WriteLine/>
...

你能用反射器做到这一点吗?

【问题讨论】:

    标签: .net reflection assemblies


    【解决方案1】:

    Reflector 能够显示导入的类型和程序集。右键单击程序集并从上下文菜单中选择分析。然后,您可以展开生成的树视图并复制依赖项的文本表示(尽管没有 XML,简单的纯文本)。例如。对于您将获得的 PresentationCore 程序集(并非所有引用都在下面的示例中展开):

    PresentationCore
      Depends On
        Microsoft.VisualC
            Microsoft.VisualC.DebugInfoInPDBAttribute..ctor()
            Microsoft.VisualC.MiscellaneousBitsAttribute..ctor(Int32)
        mscorlib
        PresentationCFFRasterizer
        System
        System.Deployment
        System.Drawing
        System.Xml
        UIAutomationProvider
        UIAutomationTypes
        WindowsBase
      P/Invoke Imports
        ADVAPI32.dll
            <Module>.CloseServiceHandle(SC_HANDLE__*) : Int32 modopt(CallConvStdcall)
            <Module>.CreateWellKnownSid(WELL_KNOWN_SID_TYPE, Void*, Void*, UInt32 modopt(IsLong)*) : Int32 modopt(CallConvStdcall)
            <Module>.InitializeSecurityDescriptor(Void*, UInt32 modopt(IsLong)) : Int32 modopt(CallConvStdcall)
            <Module>.OpenSCManagerW(UInt16 modopt(IsConst)*, UInt16 modopt(IsConst)*, UInt32 modopt(IsLong)) : SC_HANDLE__* modopt(CallConvStdcall)
            <Module>.OpenServiceW(SC_HANDLE__*, UInt16 modopt(IsConst)*, UInt32 modopt(IsLong)) : SC_HANDLE__* modopt(CallConvStdcall)
            <Module>.QueryServiceStatus(SC_HANDLE__*, _SERVICE_STATUS*) : Int32 modopt(CallConvStdcall)
            <Module>.RegCloseKey(HKEY__*) : Int32 modopt(IsLong) modopt(CallConvStdcall)
            <Module>.RegOpenKeyExW(HKEY__*, UInt16 modopt(IsConst)*, UInt32 modopt(IsLong), UInt32 modopt(IsLong), HKEY__**) : Int32 modopt(IsLong) modopt(CallConvStdcall)
            <Module>.RegQueryInfoKeyW(HKEY__*, UInt16*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, _FILETIME*) : Int32 modopt(IsLong) modopt(CallConvStdcall)
            <Module>.RegQueryValueExW(HKEY__*, UInt16 modopt(IsConst)*, UInt32 modopt(IsLong)*, UInt32 modopt(IsLong)*, Byte*, UInt32 modopt(IsLong)*) : Int32 modopt(IsLong) modopt(CallConvStdcall)
            <Module>.SetSecurityDescriptorDacl(Void*, Int32, _ACL*, Int32) : Int32 modopt(CallConvStdcall)
            <Module>.SetSecurityDescriptorSacl(Void*, Int32, _ACL*, Int32) : Int32 modopt(CallConvStdcall)
            <Module>.StartServiceW(SC_HANDLE__*, UInt32 modopt(IsLong), UInt16 modopt(IsConst)**) : Int32 modopt(CallConvStdcall)
        KERNEL32.dll
        mscms.dll
        mshwgst.dll
        MSVCR80.dll
        ntdll.dll
        ole32.dll
        penimc.dll
        PresentationNative_v0300.dll
        shfolder.dll
        urlmon.dll
        user32.dll
        WindowsCodecs.dll
        WindowsCodecsExt.dll
        wpfgfx_v0300.dll
    

    【讨论】:

    • 谢谢!是否可以通过命令行从 Reflector 获取此输出?
    • 这不是我所知道的,抱歉。
    【解决方案2】:

    获取任意程序集的引用类型和成员将是非常困难的;你需要一个深度 IL 工具——本质上是反射器。字节获取引用程序集(并观察循环引用,这可能的),类似于:

    static void Main() {
        WriteReferences(Assembly.GetEntryAssembly());        
    }
    static void WriteReferences(Assembly assembly) {
        HashSet<string> done = new HashSet<string>();
        Queue<AssemblyName> pending = new Queue<AssemblyName>();
        foreach (var name in assembly.GetReferencedAssemblies()) {
            pending.Enqueue(name);
        }
        while(pending.Count > 0) {
            var name = pending.Dequeue();
            string s = name.FullName;
            if (done.Add(s)) {
                Console.WriteLine(s);
                try {
                    Assembly asm = Assembly.Load(name);
                    foreach (var next in asm.GetReferencedAssemblies()) {
                        pending.Enqueue(next);
                    }
                }
                catch { }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多