日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

动态编译真有用武之地?

發(fā)布時(shí)間:2025/3/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 动态编译真有用武之地? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

臨近下班,一同事突然問到:“如何用DotNet動(dòng)態(tài)生成代碼的類?”噫,靜態(tài)語言要實(shí)現(xiàn)動(dòng)態(tài)生成類,有點(diǎn)意思,不過這又有什么實(shí)際的應(yīng)用呢?隨手查了一下.Net Framework中提供的類庫,看到Microsoft.Csharp命名空間,其Compiler類已經(jīng)實(shí)現(xiàn)了編譯器的托管包裝。這下方便了,先實(shí)現(xiàn)代碼看一下:

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;//編譯屬性參數(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內(nèi)容
????????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;
????????}

????}

}

個(gè)人感覺如果只是為了加強(qiáng)系統(tǒng)的靈活性而動(dòng)態(tài)生成代碼類,并無太大意義,不知哪位有經(jīng)驗(yàn)的兄弟來說兩句。

轉(zhuǎn)載于:https://www.cnblogs.com/NeoLee/archive/2005/01/05/86533.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的动态编译真有用武之地?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。