.NET 即时编译

RuiJi.Net 在地址处理上使用即时编译

我们在制作抓取软件的过程中,发现某些新闻源地址是动态变化的,例如

http://www.xxx.com/2018/05/01/roll.html

某些网站的滚动新闻页面地址会根据当前日期发生变化

所以RuiJi.Net内置了地址生成函数

例如:now(format)

在录入新闻源时,使用方式如下

http://www.xxx.com/{# now(“yyyy/MM/dd”) #}/roll.html

同时计划函数可以自行扩展,使用如下的定义方式

函数名: xxx

参数:xxx

代码 :

result = Datetime.Now.ToString({0})

描述: xxxx

即时编译器代码如下

public class JITCompile
    {
        private static string GenerateCode(string code)
        {
            return @"
            using System;
            using System.ComponentModel;
            using System.Text;
            using System.Text.RegularExpressions;
            using System.Collections;
            using System.Collections.Generic;
            using System.Reflection;

            sealed class RuiJiCompile
            {
                public static object v;
                
                public static void Main()
                {
                    
                }

                public static object Exec()
                {
                    object result;
                    " + code + @"
                    return result;
                }
            }";
        }

        private static CompilerResults Compile(string codes)
        {
            var compiler = new CSharpCodeProvider();
            var parameters = new CompilerParameters();
            parameters.WarningLevel = 4;
            parameters.GenerateInMemory = true;
            parameters.ReferencedAssemblies.Add("System.dll");

            return compiler.CompileAssemblyFromSource(parameters, codes);
        }

        public static string GetResult(string code)
        {
            code = GenerateCode(code);
            var result = Compile(code);

            if(result.Errors.HasErrors)
            {
                var es = "";
                foreach (CompilerError er in result.Errors)
                {
                    es += er.ErrorText;
                }

                return es;
            }

            Type type = result.CompiledAssembly.GetType("RuiJiCompile");
            return type.GetMethod("Exec").Invoke(null, new string[] { }).ToString();
        }
    }

using 及 引用库 在将来也计划可以自由选择

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值