The dynamic compilation and execution of code is one. NET platform provides us with a powerful tool to flexibly expand (and, of course, internal developers) complex and unpredictable logic, and to extend our existing applications with some additional code. This gives us, to a large extent, another way of extending (which, of course, is not strictly an extension, but at least gives us a clue).
Dynamic code execution can be applied to a number of occasions such as template generation, plus logical extensions. A simple example, for the Web site that response speed, HTML static page is often our best choice, but based on data-driven sites are often difficult to use static page implementation, then the dynamic page to generate HTML work may be a good application of the occasion. In addition, for some templates, we can also use it to do. In addition, this is in itself the way Plug-ins are written.
The most basic dynamic compilation
. NET provides us with a very strong support to achieve all this we can do on the basis of the main application of the two namespaces are: System.CodeDom.Compiler and Microsoft.csharp or Microsoft.VisualBasic. In addition, you need to use reflection to dynamically execute your code. The principle of dynamically compiling and executing code is to deliver the source code to CSharpCodeProvider to perform the compilation (in fact, the is no different), if there are no compilation errors, The generated IL code is compiled into a DLL that is stored in memory and loaded in an application domain (which is implied to be current) and invoked by reflection in a way that invokes one of its methods or triggers an event, and so on. It's a way to write plug-ins, and because of that, we can organize and extend our programs with predefined excuses and return them to the main program to trigger. A basic procedure for dynamically compiling and executing code includes:
1. The code that will be compiled and executed is read in and saved as a string
2. Declaring a CSharpCodeProvider object instance
3. Call the CSharpCodeProvider instance's CompileAssemblyFromSource method compilation
4. Generate an instance of the generated object with Reflection (Assembly.createinstance)
5. Call its method
The following code fragment contains the complete compilation and execution process:
-
- Get the code to compile
-
- string strsourcecode = This.txtSource.Text;
-
- 1.Create a new Csharpcodeprivoder instance
-
- CSharpCodeProvider Objcsharpcodeprivoder = new CSharpCodeProvider ();
-
- 2.Sets the runtime compiling parameters by crating a new CompilerParameters instance
-
- CompilerParameters objcompilerparameters = new CompilerParameters ();
-
- OBJCOMPILERPARAMETERS.REFERENCEDASSEMBLIES.ADD ("System.dll");
-
- OBJCOMPILERPARAMETERS.REFERENCEDASSEMBLIES.ADD ("System.Windows.Forms.dll");
-
- Objcompilerparameters.generateinmemory = true;
-
- 3.compilerresults:complile the code snippet by calling a to the provider
-
- CompilerResults cr = Objcsharpcodeprivoder.compileassemblyfromsource (objcompilerparameters, StrSourceCode);
-
- if (CR. Errors.haserrors)
-
- {
-
- String strerrormsg = cr. Errors.Count.ToString () + "Errors:";
-
- for (int x = 0; x < cr. Errors.Count; X + +)
-
- {
-
- Strerrormsg = strerrormsg + "\r\nline:" +
-
- Cr. ERRORS[X]. Line.tostring () + "-" +
-
- Cr. ERRORS[X]. ErrorText;
-
- }
-
- This.txtResult.Text = strerrormsg;
-
- MessageBox.Show ("There were build Erros, please modify your code.", "compiling Error");
-
- Return
-
- }
-
- 4. Invoke the method by using Reflection
-
- Assembly objassembly = cr.compiledassembly;
-
- Object objclass = Objassembly.createinstance ("Dynamicly.helloworld");
-
- if (objclass = null)
-
- {
-
- This.txtResult.Text = "Error:" + "couldn ' t load class."
-
- Return
-
- }
-
- object[] objcodeparms = new Object[1];
-
- Objcodeparms[0] = "Allan."
-
- String strresult = (string) objclass.gettype (). InvokeMember ("GetTime", BindingFlags.InvokeMethod, NULL, objclass, objcodeparms);
-
- This.txtResult.Text = Strresult;
-
-