动态编译真有用武之地?
生活随笔
收集整理的這篇文章主要介紹了
动态编译真有用武之地?
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
臨近下班,一同事突然問到:“如何用DotNet動態(tài)生成代碼的類?”噫,靜態(tài)語言要實現(xiàn)動態(tài)生成類,有點意思,不過這又有什么實際的應用呢?隨手查了一下.Net Framework中提供的類庫,看到Microsoft.Csharp命名空間,其Compiler類已經(jīng)實現(xiàn)了編譯器的托管包裝。這下方便了,先實現(xiàn)代碼看一下:
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;//編譯屬性參數(shù)配置
????????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];//數(shù)組長度視引用文件數(shù)量
????????????_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;
????????}
????}
}
個人感覺如果只是為了加強系統(tǒng)的靈活性而動態(tài)生成代碼類,并無太大意義,不知哪位有經(jīng)驗的兄弟來說兩句。
轉載于:https://www.cnblogs.com/NeoLee/archive/2005/01/05/86533.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的动态编译真有用武之地?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成功抢救文档
- 下一篇: **上海铁路局2004年最新时刻发布!*