1. Define an interface for reflection
Public interface irunexpression
{
Hashtable mainrun (bool isdebug );
Int precision
{Set; get ;}
}
2. Generate the DLL of the database content. Here, the content is first written to death and can be read directly from the database.
Stringbuilder _ sb = new stringbuilder ();
// Load reference
_ Sb. append ("using system; \ r \ n ");
_ Sb. append ("using system. collections; \ r \ n ");
_ Sb. append ("using system. Threading; \ r \ n ");
_ Sb. append ("using system. IO; \ r \ n ");
_ Sb. append ("using system. Text; \ r \ n ");
_ Sb. append ("using system. Data; \ r \ n ");
// Start namespace
_ Sb. append ("namespace codecompile \ r \ n ");
_ Sb. append ("{\ r \ n ");
_ Sb. append ("public class runexpression: irunexpression \ r \ n ");
_ Sb. append ("{\ r \ n ");
_ Sb. append ("Public runexpression () \ r \ n ");
_ Sb. append ("{\ r \ n ");
_ Sb. append ("} \ r \ n ");
// Simple attributes
_ Sb. append ("Private int _ precision; \ r \ n ");
_ Sb. append ("Public int precision \ r \ n ");
_ Sb. append ("{\ r \ n ");
_ Sb. append ("set {_ precision = value ;}\ r \ n ");
_ Sb. append ("Get {return _ precision;} \ r \ n ");
_ Sb. append ("} \ r \ n ");
// Simple implementation
_ Sb. append ("Public hashtable mainrun (bool isdebug) {return New hashtable () ;}\ r \ n ");
_ Sb. append ("} \ r \ n ");
_ Sb. append ("} \ r \ n ");
String codecontent = _ sb. tostring (); // the content to be compiled
// The following content requires using Microsoft. CSHARP;
Csharpcodeprovider _ scp = new csharpcodeprovider ();
Compilerparameters _ CP = new compilerparameters ();
_ CP. generateexecutable = false;
_ CP. mainclass = "codecompile. runexpression ";
// Obtain the generated path :)
Assembly a1 = assembly. getexecutingassembly ();
String e1 = a1.codebase;
Int M = e1.indexof (@ "//") + 3;
Int n = e1.length;
E1 = e1.substring (m, n-M );
String [] All = e1.split (New char [] {'/'});
String prefix = "";
For (INT I = 0; I <all. Length-1; I ++)
{
Prefix + = All [I] + "/";
}
_ CP. outputassembly = prefix + "runexpressiontest. dll ";
_ CP. includedebuginformation = true;
_ CP. referencedassemblies. Add ("system. dll ");
_ CP. referencedassemblies. Add ("system. Data. dll ");
_ CP. referencedassemblies. Add ("system. Web. dll ");
_ CP. referencedassemblies. Add ("system. xml. dll ");
_ CP. generateinmemory = false;
_ CP. WarningLevel = 3;
_ CP. treatwarningsaserrors = false;
_ CP. compileroptions = "/optimize ";
_ CP. tempfiles = new tempfilecollection (prefix + @ "\ Temp", true); // put the compiled process file to the temp directory.
Compilerresults _ Cr = _ SCP. createcompiler (). compileassemblyfromsource (_ CP, codecontent );
System. Collections. Specialized. stringcollection _ SC = _ cr. output;
Compilererrorcollection _ EC = _ cr. errors;
3. OK. The content compilation is complete. The following is a call to it.
String assemblyname = "runexpressiontest. dll ";
Objecthandle M = activator. createinstance (assemblyname, "codecompile. runexpression ");
Irunexpression _ ire = (irunexpression) M. Unwrap ();
_ Ire. Precision = 2;
Hashtable _ htreturn = _ ire. mainrun (true );