Problem:
You need to get a specificProgramAll the Assembly referenced by the set. This information tells you whether the Assembly is referencing one or more of your created assembly, or whether your assembly is referencing other specific assembly.
Solution:
UseAssembly. getreferencedassembliesMethod to obtain the Assembly referenced by an assembly. For example:
Public static string [] Builddependentassemblylist ( String Path, List < String > Assemblies)
{
// Maintain an assembly list required by this Assembly
If (Assemblies = Null )
Assemblies = New List < String > ();
// Check whether the program contains the path
If (Assemblies. Contains (PATH) = True )
Return ( New String [0]);
Assembly ASM = Null ;
// Check this path
// Check whether it is a program name or a path.
If (Path. indexof ( Path . Directoryseparatorchar, 0, path. Length )! =-1) |
(Path. indexof ( Path . Altdirectoryseparatorchar, 0, path. Length )! =-1 ))
{
// Load the Assembly from this path
ASM = Assembly . Reflectiononlyloadfrom (PATH );
}
Else
{
// Is an assembly name
ASM = Assembly . Reflectiononlyload (PATH );
}
// Add the assembly to the list
If (ASM! = Null )
{
Assemblies. Add (PATH );
}
// Obtain the referenced assembly
Assemblyname [] Imports = ASM. getreferencedassemblies ();
// Traverse all references and perform recursion
Foreach ( Assemblyname Asmname In Imports)
{
Builddependentassemblylist (asmname. fullname, assemblies );
}
String [] Temp = New String [Assemblies. Count];
Assemblies. copyto (temp, 0 );
Return (Temp );
}
This sectionCodeReturns a string array containing the original assembly, all referenced assembly, and other Assembly dependent on the Assembly.
For example:
Run this method on D: \ writename \ bin \ debug \ writename.exe and the following results will be obtained:
D: \ writename \ bin \ debug \ writename.exe
Mscorlib, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089
The writename code is as follows:
UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. text;
NamespaceWritename
{
ClassProgram
{
Static voidMain (String[] ARGs)
{
Console. Writeline ("Welcome to http://adaiye.cnblogs.com");
}
}
}
Discussion:
Retrieving the types referenced by an assembly is useful when checking which assembly is being referenced by another assembly. This understanding can help you learn how to use a new assembly. This method can also help check the dependencies between the assembly.
System. reflection. AssemblyThe getreferencedassemblies method of the class can obtain a list of all referenced assembly. This method has no parameters and returns an array of assemblyname objects instead of the array of types. Assemblyname contains the Assembly name, version, and local information (Culture Information),Public/private key pair and other information.
Note that this method is not used.Assembly. reflectiononlyload *Method to load the Assembly, because these assembly will only be checked during compilation. When Using Reflection to load the Assembly for check, you should use the reflectiononlyload * method. These methods do not allow you to execute code from the loaded assembly.