user2836291
user2836291

Reputation: 146

Get all referenced assemblies in current project

I'm trying to build method that will check all dlls that are not system dlls, and download them from FTP. For example I have some devxpress DLLs referenced and newtonsoft (Json.net). Problem is when I try to get list I only get 16 of them and there are more on reference list.

List<string> moduli = new List<string>();
Assembly a = typeof(frmPostavkeDokumenata).Assembly;
Assembly assem =Assembly.GetEntryAssembly();

foreach (AssemblyName an in assem.GetReferencedAssemblies())
{
    if (!an.Name.Contains("System") && 
        !an.Name.Contains("mscorlib") && !an.Name.Contains("Microsoft"))
    {
        moduli.Add(an.Name);
    }
}

This is my reference list:

enter image description here

Upvotes: 4

Views: 4732

Answers (2)

Emre Kabaoglu
Emre Kabaoglu

Reputation: 13146

Assembly.GetReferencedAssemblies just retrieve the referenced assemblies which are used as a Type in your Assembly. So, actually it is impossible to retrieve Visual Studio references using reflection.

This is the only way to perform it that you should parse .csproj file of assembly/project.

Upvotes: 2

Evk
Evk

Reputation: 101443

When you compile your project in visual studio - only dlls which are actually used by your project will be compiled into dll\exe metadata as referenced assemblies, regardless of which are listed in "references" section.

You have several options:

  1. Recursively build list of referenced assemblies. This will give you a list of all assemblies used by your application and all assemblies used by its dependencies, which should be a complete list of dlls you might actually need at runtime.

  2. Subscribe to AppDomain.AssemblyResolve event and provide missing dlls there (better option).

Upvotes: 1

Related Questions