Dynamically compile and call the. NET source code in the database ~

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.