VB.NET dynamic compilation (1)

zhaozj2021-02-08  210

VB.NET dynamic compilation (1)

table of Contents

(1) What is CODEDOM?

(2) Dynamic compilation of VB.NET

(3) How to dynamically perform your own code

(4) How to dynamically generate its own DLL

(5) How to dynamically generate its own EXE

(6) Advanced calls

What is CodeDom?

Everyone knows all the languages ​​of .NET, eventually compiled into IL. The intent set of IL is a public, platform-independent language. In theory, anyone can write a compiler of IL code to compile their own language into an IL language. Just know the structure of the IL instruction set and the .NET PE file.

But there is a problem in this. The .NET Framework is constantly upgrading, the IL instruction set may change. There may be new, optimized instructions to replace old instructions. For example, there are technologies such as MMX, 3DNOW. Microsoft can allow IL to support new CPU instructions by optimizing the IL compiler. At the same time, we may introduce new IL instructions to directly support new technologies. So how do you make the compiler automatically adapt and generate the corresponding optimization code?

As a developer of .NET, Microsoft proposes the general-purpose compilation interface under .NET. Any language developer, you can implement this purpose as long as this interface is implemented.

Codedom separates "Syntax Analysis" and "IL Compile" to achieve this. For example, VB.NET compiler, its compilation principle should be like this:

1. VB.NET compiler reads into source code and analyzes validity analysis

2. The compiler translate the source code to "Define Variables", "Method Definition Start (End)", "IF Branch Judgment", "Calling Method" and the abstract underlayer (note, is not IL language). These methods are defined in Codedom. *

3. The compiler calls the CODEDOM's built-in engine to compile these methods into IL code.

You can find common definitions in codedom in MS-Help: //msdnqtr.2003/ms.msdnqtr.2003apr.1033/cpgenref/html/cpconcodedomquickreference.htm.

Ok, until now, you already know the compilation principle of VB.NET. I don't know if you have noticed that all languages ​​in vs.net work in an IDE. So, how is Microsoft to implement inside an IDE, integrated with multiple grammar tools? The answer is: MS is implemented in a set of specific interfaces.

Codedom has a series of interfaces and base classes that enable your own grammar analyzer by inheritance (basis) and implementation (interface). In the System.Codedom.compiler namespace, you can see these base classes and interfaces. If you want to write your own Pascal compiler, just implement these interfaces, then write a large pile of VS.NET IDE's extension module, you can make VS.NET to become Delphi.net.

Interestingly, Microsoft also included an icodegenerator interface, I don't quite clear why Microsoft wants to define this interface, which may be required for the debugger. MSDN said that this interface is used to reverse the IL code into natural code. That is, if the compiler implements this code, you can turn the IL code into the code supported by the compiler. Through this interface, we can reverse the IL code into VB code (the VB compiler implements the interface, and I don't know if C #, the interface should also be implemented). * This is written just for easy understanding. This is actually not entirely, which is structured.

PS: These things are just the results of studying Codedom at night. If you feel that you have a conflict with your understanding, welcome to shoot bricks

转载请注明原文地址:https://www.9cbs.com/read-2559.html

New Post(0)