DxCK
DxCK

Reputation: 4552

Get paths of assemblies used in Type

I need a method that takes a Type and returns the paths of all assemblies that used in the type. I wrote this:

public static IEnumerable<string> GetReferencesAssembliesPaths(this Type type)
{   
 yield return type.Assembly.Location;

 foreach (AssemblyName assemblyName in type.Assembly.GetReferencedAssemblies())
 {
  yield return Assembly.Load(assemblyName).Location;
 }
}

Generally this method do the job, but have some disadvantages:

Upvotes: 9

Views: 5569

Answers (2)

DxCK
DxCK

Reputation: 4552

I think i solved the Assembly.Load() problem by replacing it to Assembly.ReflectionOnlyLoad().

now this is how my method looks like:

public static IEnumerable<string> GetReferencesAssembliesPaths(this Type type)
{           
    yield return type.Assembly.Location;

    foreach (AssemblyName assemblyName in type.Assembly.GetReferencedAssemblies())
    {
        yield return Assembly.ReflectionOnlyLoad(assemblyName.FullName).Location;
    }
}

now the only left problem is the type.Assembly.GetReferencedAssemblies(), how do i get referenced assemblies from the type rather than from the assembly?

Upvotes: 12

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

type.Assembly.GetReferencedAssemblies() will return all the assemblies that are referenced by the assembly in which the type is declared. This doesn't mean that the assemblies you will get with this function have anything in common with the given type.

Upvotes: 2

Related Questions