将数据库中的.net原码进行动态编译及调用~

原文:http://www.cnblogs.com/minbear/archive/2004/05/12/9058.aspx

将数据库中的.net原码进行动态编译及调用~

1、为了反射,定义一个接口
public interface IRunExpression
 {
Hashtable MainRun(bool IsDebug);
int Precision
  {set;get;}
}

2、生成数据库内容的dll,在这里将内容先写死,可以直接从数据库读取
StringBuilder _SB = new StringBuilder();
//加载引用
    _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开始
    _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");
 //简单的属性
    _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");
//简单方法实现
   _SB.Append("public Hashtable MainRun(bool IsDebug){return new Hashtable();} /r/n");
    _SB.Append(" }  /r/n");
    _SB.Append("}  /r/n");

string CodeContent = _SB.ToString();   //需要编译的内容

//下面的内容需要using Microsoft.CSharp;

CSharpCodeProvider _SCP = new CSharpCodeProvider();
    CompilerParameters _CP = new CompilerParameters();

    _CP.GenerateExecutable = false;
    _CP.MainClass = "CodeCompile.RunExpression";

    //获取生成路径:)
    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);  //把编译的过程文件放到temp目录。

    CompilerResults _CR = _SCP.CreateCompiler().CompileAssemblyFromSource(_CP,CodeContent);

    System.Collections.Specialized.StringCollection _SC = _CR.Output;

    CompilerErrorCollection _EC = _CR.Errors;

3、OK,内容编译完成,下面是对它的调用
string AssemblyName = "RunExpressionTest.DLL";
ObjectHandle M = Activator.CreateInstance(AssemblyName,"CodeCompile.RunExpression");
IRunExpression _IRE = (IRunExpression)M.Unwrap();
_IRE.Precision = 2;
Hashtable _HTreturn = _IRE.MainRun(true);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值