C # Assembly series 03, referencing multiple modules,

Source: Internet
Author: User
Tags mscorlib

C # Assembly series 03, referencing multiple modules,

We often reference assembly in projects. Generally, an Assembly contains one module, but an assembly can also contain multiple modules. This article will show you how to reference multiple modules in A. cs file that can be compiled into an. EXE executable file.

 

□Create the first module
→ In the previous two articles, the as folder of drive F stores several assembly and other files.
→ Open the "VS2012 developer command prompt" and enter the following command to clear all the content in the as folder of drive F.

→ Create MyFirstModule. cs in the as folder of drive F, use NotePad to open and write the following code, and save

using System;
class MyFirstModule
{
    public static void Hello()
    {
Console. WriteLine ("Greetings from module 1 ~ ");
    }
}

→ In "VS2012 developer command prompt", enter the following command, compile the class into a module, and press ENTER

→ Run the following command to find that the as folder of drive F contains a MyFirstModule. netmodule file.

If the →lecommand is entered, decompile the generated module, and put the illeilcode in the 1.txt file. Press enter.

Open the 1.txt file, as shown in the following command:

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.module MyFirstModule.netmodule
// MVID: {4403DD3C-6C5D-4AD7-AAD4-2929F36C0F4F}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x00440000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit MyFirstModule
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Hello() cil managed
  {
// Code size 13 (0xd)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      bytearray (65 67 EA 81 6D 00 6F 00 64 00 75 00 6C 00 65 00   // eg..m.o.d.u.l.e.
                                    20 00 31 00 84 76 EE 95 19 50 7E 00 )             //  .1..v...P~.
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method MyFirstModule::Hello
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
// Code size 7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method MyFirstModule::.ctor
} // end of class MyFirstModule

○. Assembly extern mscorlib, indicating that the module uses an external mscorlib assembly
○. Module MyFirstModule. netmodule, indicating that modlue is named MyFirstModule. netmodule
○. Class..., indicates the class in the module.

 

□Create the Second module

→ Create MySecondModule. cs in the as folder of drive F, write the following code in notepad, and save

using System;
class MySecondModule
{
    public static void Hello()
    {
Console. WriteLine ("Greetings from module 2 ~ ");
    }
}

→ In "VS2012 developer command prompt", enter the following command, compile the class into a module, and press ENTER

 

□Reference multiple modules

→ Create MainClass. cs in the as folder of drive F, write the following code in notepad, and save

using System;
class MainClass
{
    static void Main()
    {
        MyFirstModule.Hello();
        MySecondModule.Hello();
    }
}

→ Run the following command to compile MainClass. cs and introduce two modules.

Enter the following command to run mainclass.exe:

Example: Enter the following command to decompile mainclass.exe, and present the ilcode to the 2.txt file.

Open the 2.txt file, as shown in the following command:

// Metadata version: v4.0.30319
.module extern MyFirstModule.netmodule
.module extern MySecondModule.netmodule
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly MainClass
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                             63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}
.file MyFirstModule.netmodule
    .hash = (2F 9D 95 85 6E F4 D2 CA 50 61 C0 9F A1 58 C6 5F   // /...n...Pa...X._
             5D 1D 96 4B )                                     // ]..K
.file MySecondModule.netmodule
    .hash = (28 72 47 EE 2E 76 45 AA 6B 87 17 93 6B 4F 1C 5D   // (rG..vE.k...kO.]
             84 D8 07 CB ) 
.module MainClass.exe
// MVID: {FD4741CB-DEDE-4D91-9B95-16DAF3CEB47E}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x003A0000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit MainClass
       extends [mscorlib]System.Object
{
  .method private hidebysig static void  Main() cil managed
  {
    .entrypoint
// Code size 14 (0xe)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  call       void [.module MyFirstModule.netmodule]MyFirstModule::Hello()
    IL_0006:  nop
    IL_0007:  call       void [.module MySecondModule.netmodule]MySecondModule::Hello()
    IL_000c:  nop
    IL_000d:  ret
  } // end of method MainClass::Main
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
// Code size 7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method MainClass::.ctor
} // end of class MainClass

○. Module extern MyFirstModule. netmodule, which references the module MyFirstModule
○. Module extern MySecondModule. netmodule, which references the module MySecondModule
○. Assembly extern mscorlib, which references the. NET default assembly mscorlib
○. Assembly MainClass, representing the current assembly
○ Module mainclass.exe, which indicates that mainclasscontains a module named mainclass.exe.
○. Class..., indicating the Assembly class

 

"C # Assembly series" includes:

C # Assembly series 01, use NotePad to write C # And IL code, use the DOS command to compile the assembly, and run the C # Assembly series 02 program, use NotePad to view the IL code C # Assembly series 03 of the executable assembly, reference multiple module C # Assembly series 04, understand the keyword internal C # Assembly generation 05 when the Assembly contains multiple modules, so that the Assembly contains multiple modules

 

References:

Http://www.computersciencevideos.org/created by Jamie King


C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

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.