博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将数据库中的.net原码进行动态编译及调用~
阅读量:6928 次
发布时间:2019-06-27

本文共 2545 字,大约阅读时间需要 8 分钟。

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

转载地址:http://htkjl.baihongyu.com/

你可能感兴趣的文章
WinForm 跨进程传输数据
查看>>
[zz]HDFS文件系统和OpenStack swift对象存储有何不同
查看>>
jdk安装配置
查看>>
数据类型范围》
查看>>
Miller-Rabin素数测试学习笔记
查看>>
Wireshark运行问题:"The NPF driver isn’t running…"(NPF驱动没有运行)
查看>>
STM32使用ST库新建工程
查看>>
我们工作到底为了什么
查看>>
关于“VisualSVN-2.5.2”的破解
查看>>
用jQuery判断两个元素是否有重叠部分
查看>>
windows 下安装Simplejson方法
查看>>
IE并发连接限制(as)AS队列加载类(as3和as2)
查看>>
转:Android View.post(Runnable )
查看>>
ChinaTest第二天
查看>>
图灵等价和图灵完备
查看>>
CSS中position的absolute和relative的应用
查看>>
对 makefile 中 二次扩展的一点理解
查看>>
SET XACT_ABORT on
查看>>
记录mysql性能查询过程
查看>>
数据连接 DataDirectory 中的作用
查看>>