Dynamically compile and call the .NET origin in the database ~

xiaoxiao2021-03-06  54

Original: http://www.cnblogs.com/minBear/Archive/2004/05/12/9058.aspx

Dynamically compile and call the .NET origin in the database ~

1. In order to reflect, define an interface public interface irunexpression {haShtable mainrun (Int Precision {set; get;}} 2, generating DLL of the database content, here to write the content first, you can 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 "); // namespace begins _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 attribute _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 method implementation _SB.App END ("public hashtable mainrun (" Bool isdebug) {return new hashtable ();} / r / n "); _sb.append ("} / r / n "); _sb.append ("} / r / n ") String codeContent = _sb.tostring (); // The content you need 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";

// Get the generated path :) assembly a1 = askEMBLY.GETEXECUTISSEMBLY (); 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

_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.TREATWARNINGSERRORS = false; _cp.compilerOptions = "/ optimize"; _cp.tempfiles = new TempFileCollection (Prefix @ "/ TEMP", true); // Put the compiled process file in the TEMP directory.

CompilerResults _cr = _scp.createcompiler (). CompileAsessemblyFromSource (_CP, CODECONTENT);

System.collections.Specialized.StringCollection _sc = _cr.output;

CompilerErrorCollection _EC = _CR.Errors; 3, OK, compiled content, is below its invocation string AssemblyName = "RunExpressionTest.DLL"; ObjectHandle M = Activator.CreateInstance (AssemblyName, "CodeCompile.RunExpression"); IRunExpression _IRE = ( Irunexpression) m.unwrap (); _ IRE.PRECISION = 2; Hashtable_htreturn = _IRE.MAINRUN (TRUE);

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

New Post(0)