How to get Names of DLLs used by application

前端 未结 6 969
旧巷少年郎
旧巷少年郎 2021-01-11 09:26

I\'m looking the way to read all assemblies (.dlls) used by my app.

In a standard C# project there is \"References\" folder, when it is expanded I can read all lib

6条回答
  •  时光取名叫无心
    2021-01-11 09:57

    To do this properly, you need to walk the assemblies, picking up the dependencies... if your exe needs Dll_A, and Dll_A needs Dll_B (even if the exe doesn't reference it), then your exe also needs Dll_B.

    You can query this (on any assembly) via reflection; it takes a little work (especially to guard against circular references, which do happen; here's an example that starts at the "entry assembly", but this could just as easily be any assembly:

        List refs = new List();
        Queue pending = new Queue();
        pending.Enqueue(Assembly.GetEntryAssembly().GetName());
        while(pending.Count > 0)
        {
            AssemblyName an = pending.Dequeue();
            string s = an.ToString();
            if(refs.Contains(s)) continue; // done already
            refs.Add(s);
            try
            {
                Assembly asm = Assembly.Load(an);
                if(asm != null)
                {
                    foreach(AssemblyName sub in asm.GetReferencedAssemblies())
                    {
                        pending.Enqueue(sub);
                    }
                    foreach (Type type in asm.GetTypes())
                    {
                        foreach (MethodInfo method in type.GetMethods(
                            BindingFlags.Static | BindingFlags.Public |
                                 BindingFlags.NonPublic))
                        {
                            DllImportAttribute attrib = (DllImportAttribute)
                                Attribute.GetCustomAttribute(method,
                                    typeof(DllImportAttribute));
                            if (attrib != null && !refs.Contains(attrib.Value))
                            {
                                refs.Add(attrib.Value);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }
        refs.Sort();
        foreach (string name in refs)
        {
            Console.WriteLine(name);
        }
    

提交回复
热议问题