C # in the dynamic compilation application

xiaoxiao2021-03-06  59

Original: http://www.ccw.com.cn/htm/center/prog/02_10_23_3.asp

Wuhan Manufacturing Information Technology Technology Co., Ltd. Du Sheng

A friend who did ASP asked me: "How to perform dynamic compilation in ASP.NET, just like in ASP, generate application files by the program, then run directly."

I can't help but laugh, I can think about it, in a flexible and efficient

In the system, we really need to follow certain rules to dynamically generate some programs, then afford new tasks by new programs, so that the efficiency of running is relatively high. Of course, you can also use the same program to be done using the same program, but this seems to use HTML and use the application as the main browsing format. Of course, the HTML browsing is faster and efficient.

So I answered him to write the code directly in the ASPX file, do not use CodeBehind technology, he thought for a long time, no, that code did not see it.

It is necessary to ensure that the security of the code is to meet the efficiency, so I began to think about this problem, I feel that there must be in-depth research, and after exploring, some experience will be announced.

Everyone knows that files with codebehind technology in ASP.NET must be compiled first before they can be executed. We generally compile them in vs.net or command lines, then the current problem is to compile in the program, which is to dynamically generate some program source files by doing a good rule, and then compile it. View the class library provided in the .NET Framework, found Microsoft.csharp namespace, with a class Compiler. To call this class library, you must first add D: /Winnt/Microsoft.Net/framework/v1.0.3705/cscompmgd.dll, which is the path on my machine, and you can set it according to your own machine.

Careful study COMPILER: No initialization, directly using the Compile method, which contains 5 parameters.

String [] SourceTexts Source Code Array Each value contains the source code in a file

String [] SourceTextNames Source File Name All values ​​contain a file name and the value in the above source code array corresponds.

String Target output file name

String [] Imports reference class library number is also the file path of the class libraries we referenced in vs.net, which is generally a DLL file, one of which represents a reference file.

The IDictionary Options parameter setting, actually this is also an array, which can contain multiple parameters, and the specific parameters I will talk below.

This method of actual compile requires us to enter these conditions:

1. Source code - this is a must, no source code is unable to compile, then the source code requires us to read from the program file.

2, the name of the source file, and the source code to read.

3, the file name of the compilation and output, can also not fill

4, referenced class library files, such as system.dll, system.data.dll often usage.

5, other parameter settings

Step 1: Create a class and call method

First we create a Class

Using system;

Using system.io;

Using system.text;

USING Microsoft.csharp;

Namespace mytest

{

Private string [] fileList; // file list

Private string [] cscode; // source code

Private string [] filename; // file name private string [] import; // reference class library

Public class mycomplier ()

{

/ / I have made a simple way, requiring the user to enter two parameters, one is the absolute path of the folder

The other is the output file path

Public CompileRerror [] MyCompile (String RealPath, String Output)

{

// ..........................

}

}

}

Then we will step on how to implement this method step by step.

Step 2: Read the list of program files

Due to use CodeBehind, it is actually compiled to the extension of the file named CS. First read the list of files, make a method to read the file path in the directory: Search the file through the absolute path input by the user.

Public String [] getFileList (String realpath) // Enter an absolute path

{

Return system.io.directory.Getfiles (RealPath, "*. CS"); // Enter search criteria

}

This method passes the real directory to be compiled, and then returns to the file list list that extends the CS in the directory. Then read the list of read files will prepare for the acquisition and file name of the code in the file.

Step 3: Read the contents of the program file

/ / Here we call the second step to get a list of files

FILELIST = getFileList (realpath); // Get a list of file paths

/ / Create an array according to the number of documents

Cscode = new string [filelist.length]; // Establish an array of source code

String [] filename = new string [filelist.length]; // set up a source file name

// Get the information of each file through the loop, each increase in the corresponding array

For (int i = 0; i

{

String filepath = filelist. GetValue (i) .tostring (); // Get a single file path

CSCode [i] = readxtFile (filepath, encoding.getencoding ("GB2312")); // Read the code in the source file, the encoded here is GB2312, or it can be set to other, which is called one of us created below. method

FileName [i] = system.io.path.getFileName (filepath); // Get file name

}

// Read text file content

Public String ReadtxtFile (String Path, Encoding Encode)

{

String str = "";

Try

{

File.exists (PATH))

{

StreamReader SR = New StreamReader (Path, Encode);

Str = sr.readtoend ();

sr.close ();

}

}

Catch (IOException EX)

{Console.Write (ex. TString ());

Return Str;

}

After the second step, we have met the two parameters required for the Compile method, then the next will be some configurations for the output of the file and references.

Step 4: Configure the output file name and path

Here you can set files and paths here, for example: e: /test/bin/test.dll string output = "e: //test//bin/test.dll";

Here we operate according to the path input path.

Step 5: Configure the reference library file

Then you must join the array as long as you are a class library you referenced by the program.

Import = new string [10]; // Define an array, the array length determines the number of files in accordance with the file you need, IMPORT [0] = "d: //winnt//microsoft.net//framework//v1.0.3705///wwn /System.dll ";

Import [1] = "D: //winnt//microsoft.net/framework//v1.0.3705//iehost.dll";

Import [2] = "D: //winnt//microsoft.net/framework//v1.0.3705//system.data.dll";

Import [3] = "D: //winnt//microsoft.net//framework//V1.0.3705//system.drawing.dll";

Import [4] = "D: //winnt//microsoft.net//framework//v1.0.3705//system.web.dll";

Import [5] = "D: //winnt//microsoft.net//framework//v1.0.3705//system.xml.dll";

Import [6] = "D: //winnt//microsoft.net/framework//v1.0.3705//cscompmgd.dll";

The above is some class libraries we often use, in addition to this, some class libraries you have written, if it is also quoted, then you must also add to an array.

Step 6: Configuring attribute parameters

Here the system provides us with an interface: iDictionary, this interface can be implemented using the following classes.

ListDictionary implements IDictionary using a single link list. It is recommended to usually contain a collection of 10 or 10 below.

HybridDictionary is used in a collection, using ListDictionary to implement iDictionary, then switch to HashTable when the collection is large.

There are others, I don't feel it, I will not talk about it. So, let's take a look:

System.collections.idictionary configs = new system.collections.specialized.listDictionary (); // Implement this interface

Configs.add ("Target", "library"); // Configure the output file type for library file, this parameter is required

Where "Target" is the format property of the file, "library" is the value of the property.

Other properties are:

AddModule Import metadata To include a list of modules in this assembly. The module name must be separated by vertical line or pipeline character. The type of value must be String.

At the base address of the baseaddress library. The type of value must be uint32

BugReport generates an error report file. The type of value must be String.

Checked Check Integer Algorithm Set the default expression to be selected (or unchecked). The type of value must be Boolean.

Debug issues the type of debug information value must be Boolean. You cannot specify "all" or "pdbonly".

LIB Specifies the additional path to search for the program set reference position lookup module and referenced program set. The type of value must be String.

Unsafe Enables unsafe mode to allow unsafe construction. The type of value must be boolean

Win32Con automatically generated Win32 resource WIN32 icon. The type of value must be String

Some of the main parameters here, and some are used less, then they are not listed. Step 7: Program compilation

CompileRerror [] CE = Compiler.compile (Cscode, FileName, Output, Import, Configs);

So actually our final compilation is to call this method, this method will return an array containing error information. The error information during the compilation process is recorded. If the error message returns, the compilation cannot be passed.

Finally, we will integrate the methods in the first step:

Public CompileRerror [] MyCompile (String RealPath, String Output)

{

// ..........................

The parameters are obtained by the above method, and finally perform compilation.

CompileRerror [] CE = Compiler.compile (Cscode, FileName, Output, Import, Configs);

Return Ce;

}

Here, basically the entire compilation process is completed, you can call or call in the ASPX page or in other classes, compiled effects and vs.net is not bad, and the speed is also very fast. Everyone can even build a web-based simple integrated development environment, of course, this is a later words.

Finally, if you don't forget to delete the source file, this is very simple, I don't talk much.

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

New Post(0)