The company's original project two development methods mainly using SQL, basically can also meet customer requirements, the advantage is simple to use, as long as familiar with SQL statements can be manipulated, the disadvantage is that too many restrictions, the need for the database at the bottom of the understanding, easy to use error, can not directly invoke business layer code, Research on the dynamic compilation of. NET, feel it to do two times the development effect should be good.
First we do a demo to explain the dynamic compilation, the following code means to organize a source string, and then compile execution.
Dynamically compiling simple code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.csharp;
Namespace ConsoleApplication6
{
Class program
{
//c# code provider
private static Codedo Mprovider comp = new CSharpCodeProvider ();
//parameter to invoke compiler
private static compilerparameters CP = new CompilerParameters ();
private static MethodInfo mi;
static void Main (string[] args)
{
StringBuilder codebuilder = new StringBuilder (); codebuilder.appendline ("Using System;");
Codebuilder.appendline ("public class MyClass");
Codebuilder.appendline ("{");
Codebuilder.appendline ("public void Hello ()");
Codebuilder.appendline ("{");
Codebuilder.appendline ("Console.WriteLine" ("hello\"); ");
Codebuilder.appendline ("}");
Codebuilder.appendline ("}");
//Join the assembly that needs to be referenced
CP. Referencedassemblies.add ("System.dll");
CompilerResults cr = Comp. CompileAssemblyFromSource (CP, codebuilder.tostring ());
//If there is a compilation error
if (CR. Errors.haserrors)
{
foreach (CompilerError item in CR. Errors)
{
Console.WriteLine (item. ToString ());
}
}
Else
{
Assembly a = cr. compiledassembly; Gets the compiled assembly
Type t = a.gettype ("MyClass"); Using reflection to get the type
Object mode = A.createinstance ("MyClass");
Mi = t.getmethod ("Hello", BindingFlags.Instance | BindingFlags.Public);
mi. Invoke (mode, new object[0]); Execute method
}
}
}
}