Having introduced the basics of CIL, we are now studying the actual use of CIL programming, starting with the pros and cons of the project.
Positive and negative to the project
It is already known that you can use Ildasm.exe to view the CIL code generated by the C # compiler (see the first article in the. NET CIL series: CIL introduction and Getting started), But maybe not. Ildasm.exe also allows you to export CIL in an assembly that is loaded into Ildasm.exe into an external file. Once you have CIL code, you can use the CIL compiler to ilasm.exe arbitrary edits or recompile code.
Description: Reflector.exe can be used to view CIL code for an assembly, or to translate CIL code into close C # code. However, if the assembly contains a CIL structure that does not have an equivalent C # implementation (C # and VB only implement a subset of all of the CIL attribute sets), we can only use Ildasm.exe.
This technique is called positive and negative engineering (round-trip-engineering), and it will be useful in the following cases.
n need to modify an assembly with no source code
n is in use. NET language compilers are not perfect enough to produce some inefficient CIL code that the user wishes to modify.
n Users build assemblies that can interoperate with COM and want to supplement those that are lost during conversion, such as COM's [helpstring] attributes.
To explain the process of positive and negative engineering, we use a text editor to create a new C # code file (HelloProgram.cs) and define the following classes (or you can use visual Studio 2008, but remember to delete AssemblyInfo.cs this file to reduce the number of CIL code generated):
// 简单的C#控制台程序
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello CIL code!");
Console.ReadLine();
}
}