开发者

Need a tool to dump the referenced types and members for an assembly

I need a tool that can dump the referenced types used by an assembly in a m开发者_StackOverflow中文版achine readable format.

e.g. this Code in assembly 'dummy.exe'

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

would produce something like

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

Can you do this with reflector?


Reflector is capable of showing the imported types and assemblies. Right-click the assembly and choose Analyze from the context menu. You can then expand the generated tree view and copy a textual representation of the dependenies (no XML though, simple plain text). E.g. for the PresentationCore assembly you would get (not all references are expanded in the sample below):

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


Getting the referenced types and members for an arbitrary assembly would be extremely hard; you'd need a deep IL tool - essentially reflector. Byt to get the references assemblies (and watching for cyclic references, which are possible), something like:

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 { }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜