搜私服
设置首页 | 加入收藏
网站首页 代码连接 游戏家族 发布家族 工具下载 技术文章
火影忍者 站内公告 私服小偷 在线留言 新登陆器 广告联系
 
   相关内容
   传奇私服【搜服宝贝】-私服发布-传奇私服-合击私服-变态私服-变态传奇-最新传奇英雄合击版-中国最大的sf发布站-火影忍者-动漫在线-变态合击传奇-每日最新传奇sf网-变态传奇私服发布网技术文章网页编程
简单了解.NET中的动态编译机制
发布时间: 2007年11月20日 人气: 出处 | 作者: / 责任编辑: admin
支持本站发展,立即购买会员装备 | 在线投稿 | 游戏玩家社区      
说来惭愧,周末跟一个同事讨论.NET中的动态编译,觉得实现起来可能有困难。一直不知道.NET Framework提供了一个代码文档对象模型(CodeDOM),可以动态的生成代码并动态编译,只是自己孤陋寡闻,没有用过罢了。这里简单的写一下实现步骤,有时间我会详细地写一下这方面的东西。
第一步:添加引用
using System.Reflection;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
第二步:创建需要编译的代码
string MyCodeString=@"
public class MyTest
{
   public static string GetTestString()
    {
        string MyStr = ""This is a Dynamic Compiler Demo!"";
        return MyStr;
    }
}";
第三步:实现动态编译
CompilerParameters compilerParams = new CompilerParameters();
       
 ///编译器选项设置
compilerParams.CompilerOptions = "/target:library /optimize";
 
///编译时在内存输出
compilerParams.GenerateInMemory = true;
 
///生成调试信息
compilerParams.IncludeDebugInformation = false;
       
 ///添加相关的引用
compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
compilerParams.ReferencedAssemblies.Add("System.dll");
 
ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
 
///编译
CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams,MyCodeString);
第四步:输出编译结果
///创建程序集
Assembly asm = results.CompiledAssembly;
       
///获取编译后的类型
object objMyTestClass = asm.CreateInstance("MyTest");
Type MyTestClassType=objMyTestClass.GetType();
 
///输出结果
Console.WriteLine(MyTestClassType.GetMethod("GetTestString").Invoke(objMyTestClass,null));
Console.ReadLine();
至此,一个简单的动态编译程序就完了,我们看到输出的结果为:
This is a Dynamic Compiler Demo!
完整的程序清单:
using System;
 
using System.Reflection;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
 
public class MyDynamicCompiler
{
    public static void Main()
    {  
        ///需要编译的字符串
        string MyCodeString=@"
        public class MyTest
        {
            public static string GetTestString()
            {
                string MyStr = ""This is a Dynamic Compiler Demo!"";
                return MyStr;
            }
        }";
 
        CompilerParameters compilerParams = new CompilerParameters();
       
        ///编译器选项设置
        compilerParams.CompilerOptions = "/target:library /optimize";
 
        ///编译时在内存输出
        compilerParams.GenerateInMemory = true;
 
        ///生成调试信息
        compilerParams.IncludeDebugInformation = false;
       
        ///添加相关的引用
        compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
        compilerParams.ReferencedAssemblies.Add("System.dll");
 
        ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
 
        ///编译
        CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams,MyCodeString);
       
        ///创建程序集
        Assembly asm = results.CompiledAssembly;
       
        ///获取编译后的类型
        object objMyTestClass = asm.CreateInstance("MyTest");
        Type MyTestClassType=objMyTestClass.GetType();
 
        ///输出结果
        Console.WriteLine(MyTestClassType.GetMethod("GetTestString").Invoke(objMyTestClass,null));
        Console.ReadLine();
    }
}
 
评论人 评论内容 ( 查看全部评论 ) 发表时间
评 论:
最多255字。
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
  • 网友评论只代表其个人观点,与本站无关
笔 名:
广告服务 | 联系我们 | 设为首页 | 收藏本站
拒绝盗版游戏 注意自我保护 谨防受骗上当 适度游戏益脑 沉迷游戏伤身 合理安排时间 享受健scri康生活
搜服宝贝承诺:为GM打造一个公平的发布平台,减少GM的开支,给GM最高的人气!
品牌商标、文字、形象及内容归 45Baby 搜服宝贝 版权所有,任何组织或个人未经 45Baby 搜服宝贝 同意,不得使用和传播。
注释:本站发布的所有游戏.均来自互联网与本站无关.请玩家仔细辨认游戏的真实性.避免有些不法的游戏业主.避免上当受骗!
特别鸣谢:徐州三十七度网络科技提供服务器集群和安全防护及网络办公环境 Http://Www.37Idc.Com.Cn Http://Www.37Idc.Net
Http://Www.45Baby.Com 搜服宝贝 版权所有 (注:本站只投放盛大区域代理传奇) 备案信息:苏ICP备08004100号