[C # recipe] [Sichuan food] menu 1: List referenced assembly

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.