Assembly Learning Experience (Part 2)

Source: Internet
Author: User

Part 2 (example)

The following is a complete example to illustrate the concepts mentioned above one by one to deepen understanding. The example contains seven files (in the main directory ):
The main directory is... /Assembly ---- source program directory
... /ASSEMBLY/bin ---- directory of the compilation result output, that is, the main directory of the application.

File Name
Type
Description

App. CS
Code source file
Main Program, including program entry,
Belongs to namespace1

Classa. CS
Code source file
Type A, including a static method,
Belongs to namespace1

Classb. CS
Code source file
Type B, including a static method,
Belongs to namespace2

Assemblyinfo. CS
Code source file
Contains Assembly signature information,
Version Information

App. Key
Public Key/private key pair File
Used to sign an assembly,
Generate strongly-named assembly

App. publickey
Contains only common keys
Only store the total key,
Use sn.exe to view

App.exe. config
XML format configuration file
App.exe application configuration file

Source code

App. CS

Using system;
Namespace namespacea
{
Public class app
{
Static void main (string [] ARGs)
{
System. Console. writeline (classa. showme ());
System. Console. writeline (namespaceb. classb. showme ());
}
}
}

Classa. CS

 

Using system;
Using system. collections;
Namespace namespacea
{
Public class classa
{
Public static string showme ()
{
Return "this is classa ";
}
}
}

Classb. CS

 

Using system;
Using system. collections;
Namespace namespaceb
{
Public class classb
{
Public static string showme ()
{
Return "this is classb ";
}
}
}

Assemblyinfo. CS

 

//////////////////////////////////////// ////////////////////////////////////////
// Module: assemblyinfo. CS
//////////////////////////////////////// ////////////////////////////////////////
Using system. reflection;
//////////////////////////////////////// ////////////////////////////////////////
// Set companyName, legalcopyright, and legaltrademarks
[Assembly: assemblycompany ("app company")]
[Assembly: assemblycopyright ("Copyright (c) 2004 @ app company")]
[Assembly: assemblytrademark ("app is a test only program")]
//////////////////////////////////////// ////////////////////////////////////////
// Set productname and productversion
[Assembly: assemblyproduct ("app product")]
[Assembly: assemblyinformationalversion ("1.0.0.0")]
//////////////////////////////////////// ////////////////////////////////////////
// Set fileversion and assemblyversion

[Assembly: assemblyfileversion ("1.0.0.0")]
[Assembly: assemblyversion ("1.0.0.0")]
[Assembly: assemblytitle ("App Type assembly")]
[Assembly: assemblydescription ("app aassembly is a test only assembly")]

//////////////////////////////////////// ////////////////////////////////////////
// Set Culture
[Assembly: assemblyculture ("")]
[Assembly: assemblydelaysign (false)]
[Assembly: assemblykeyfile ("app. Key")]
[Assembly: assemblykeyname ("")]

1. Compile the source code into a managed module)
CSC/out: Bin/classa. Module/T: module classa. CS
Parameters:
/Out: Output path
/T: output format. There can be four types:
Library ---- DLL assembly
EXE ---- console executable program (also a type of assembly)
Winexe ---- windows executable program (also a type of assembly)
Module ---- managed module (part of the Assembly)

Note: the extension of the hosting module can be any (or none), because it is read in metadata format during compilation.

2. Compile the source code into an assembly)
Compile classb into a single file assembly
CSC/out: Bin/classb. dll/T: Library classb. CS

3. Compile app. CS, classa. module, and classb. DLL into a multi-file assembly.
CSC/out: Bin/app.exe/T: EXE app. CS/addmodule: Bin/classa. Module/R: Bin/classb. dll
Parameters:
/Addmodule: add the hosted module to the Assembly/R: Add reference Description: The Assembly generated above is non-strongly-named because it has not passed the Public Key/private signature. The app.exe list of the generated Assembly contains only the description of the classa. Module hosting module, and does not include metadata of classa.module. the app.exe and classa. Moudle must be in the same directory. When app.exe is running. the type reference in module will go to classa. find the moudel file, if classa. if the moude file does not exist, system. io. filenotfoundexception. If app.exeis not used in class.module, the existence of classa.modulewill not affect the execution of app.exe (this is one of the advantages of the Assembly mentioned above, and assembly is a logical combination ).
You can also put shards together in other sub-directories in the main directory. (You can use the application to change the configuration file later to redirect the reference of classb. dll ).

Files under the generated directory:

Note that the classb. dll version is 0.0.0.0.

Execute the app.exe file:

4. modify the application configuration file (app.exe. config) to redirect the reference to classb. dll.

Now app.exe, classa.moudleand classb.dllare all in the bindirectory. app.exe will find all the types it needs during runtime, so it runs normally.

If you create a new directory under the bindirectory, such as a subdirectory, and move classb.dllto the subdirectory, then running app.exe will cause an error. In the same example, system.io.filenotfoundexceptionis incorrect because the classb type required by app.exe cannot be found. In this case, you need to change the add (if not) or the application configuration file. The application configuration file is an XML format configuration file, and the Web. the Config File serves almost the same purpose, and is used to configure the behavior of the application program running.

Note: The configuration file name must be an application name plus a. config file, and must be in the same directory.

The code for the app.exe. config file is as follows:

<Configuration>
<Runtime>
<Assemblybinding xmlns = "urn: Schemas-Microsoft-com: ASM. V1">

<Probing privatepath = "sub"/>

</Assemblybinding>
</Runtime>
</Configuration>

This file was found on msdn. I have not tried other formats. Only in this way can the file be written.

Note:
When the CLR needs to locate a dataset, it will scan several subdirectories of the application. The following is the sequence in which the CLR scans a dataset:
... /ASSEMBLY/bin/classb. dll.
... /ASSEMBLY/bin/classb. dll.
... /ASSEMBLY/bin/SUB/classb. dll.
... /ASSEMBLY/bin/SUB/classb. dll.
... /ASSEMBLY/bin/classb. EXE.
... /ASSEMBLY/bin/classb. EXE.
... /ASSEMBLY/bin/SUB/classb. EXE.
... /ASSEMBLY/bin/SUB/classb. EXE.

NOTE: If app.exe references a strongly-named assembly, CLR first searches for the Assembly in GAC and then searches for the Assembly in the above Order.

4. Create and view public/private key pair files
You can use the. NET sdktool (sn.exe) to create a public/private key pair file. First, create a public/private key pair file.
Sn-K app. Key
Then, use this file to create a file that only contains the common key:
Sn-P app. Key app. publickey
Use the-TP parameter to view
Sn-TP app. publickey

5. Create a strongly-named assembly
With the public key/private key pair, it is easy to create a strongly-named assembly. You only need
System. reflection. assemblykeyfileattribute can be added to the source code.
[Assembly: assemblykeyfile ("app. Key")]
It is generally added to the assemblyinfo. CS file.
Now, re-build classb. CS to obtain a strongly-named assembly:
CSC/out: Bin/classb. dll/T: Library classb. CS assemblyinfo. CS

Run ildasm.exe to check whether the public key in assembly has a large string value. This is the public key of the Assembly, which guarantees the uniqueness of the whole sequence set.

Manifest of classb. DLL without public key:

Manifest with public key:

6. Add the strongly-named Assembly classb. DLL to GAC.
Use gacutil.exe
Add classb. DLL to GAC:
Gacutil/I classb. dll
Delete classb. dll and re-build app.exe:
CSC/out: Bin/app.exe/T: EXE app. CS/addmodule: Bin/classa. Module/R: classb. dll
App.exe runs correctly, indicating that classb. dll has been successfully added to GAC and becomes a shared assembly.

Remove classb. dll from GAC: gacutil/u classb
Note:
You cannot specify the extension when removing an assembly (because each assembly in GAC is a directory instead of an actual assembly ).

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.