利用.NET语言开发自己的脚本语言(二)--动态编译

动态编译:
using System;
using System.IO;
using System.Text;
using Microsoft.CSharp;

namespace TestProject
{
public class DynamicCompile
{
private String _physicspath = @"D:/Project.Net/TestProject/DcCodeFile/";//文件夹的路径
private String _outputpath = @"D:/Project.Net/TestProject/bin";//输出文件的路径

private String[] _filelist; //文件列表
private String[] _cscode; //源码
private String[] _filename; //文件名
private String[] _import; //引用
private System.Collections.IDictionary configs = null;//编译属性参数配置

public DynamicCompile()
{}

public CompilerError[] ExCompiler()
{
Fill_cscode();
Fill_import();
Fill_configs();
CompilerError[] ce= Compiler.Compile(_cscode,_filename, _outputpath,_import, configs);
return ce;
}
private String[] Fill_filelist()
{
return System.IO.Directory.GetFiles(_physicspath,"*.cs");
}
private void Fill_cscode()
{
_filelist= Fill_filelist();
_cscode=new String[_filelist.Length];
_filename=new String[_filelist.Length];
for(int i=0;i<_filelist.Length;i++)
{
String filepath= _filelist.GetValue(i).ToString();//获取文件路径
_cscode[i]= ReadCodeFile(filepath, Encoding.GetEncoding("utf-8"));
_filename[i]= System.IO.Path.GetFileName(filepath);
}
}
private void Fill_import()
{
_import=new String[10];//数组长度视引用文件数量
_import[0]=@"C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/System.dll";
_import[1]=@"C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/System.Web.dll";
}
private void Fill_configs()
{
configs=new System.Collections.Specialized.ListDictionary();
configs.Add("target","library");
}
//读取cscode内容
private String ReadCodeFile(String path,Encoding encode)
{
String content=String.Empty;
try
{
if (File.Exists(path))
{
StreamReader sr=new StreamReader(path,encode);
content=sr.ReadToEnd();
sr.Close();
}
}
catch(IOException ex)
{
Console.Write(ex.ToString());
}
return content;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值