• c# 中的evel


    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;

    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.Reflection;

    namespace CSHARP_EVAL_FUNCTION
    {
    public class EVAL
    {
    private static string prefix = @"using System;
    public static class DynamicClass
    {
    public static void Bomb()
    {";

    public static string postfix = @"}}";

    public string content { get; set; }

    public void Eval()
    {
    if (content == "")
    {
    Console.WriteLine("必须为Content属性赋予值");
    return;
    }
    string code = prefix + content + postfix;
    CompilerResults result = null;

    using (var provider = new CSharpCodeProvider())
    {
    var options = new CompilerParameters();
    options.GenerateInMemory = true;

    result = provider.CompileAssemblyFromSource(options, code);

    if (result.Errors.HasErrors)//编译有错误
    {
    var errorMsg = new StringBuilder();
    foreach (CompilerError error in result.Errors)
    {
    errorMsg.AppendFormat("Line:{0},Column:{1},Content:{2}", error.Line, error.Column, error.ErrorText);
    }
    Console.WriteLine(errorMsg.ToString());
    }
    else//运行类 DynamicClass 中的HelloWorld方法
    {

    Type dynamicClass = result.CompiledAssembly.GetType("DynamicClass");
    dynamicClass.InvokeMember("Bomb", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, null);
    }
    }

    }
    }
    }

    http://blog.csdn.net/ghostbear/article/details/7764510

  • 相关阅读:
    笨笨走了
    WSE 3.0 文档翻译:WSE架构
    系列文章索引
    WSE 3.0 文档翻译:WSE的新功能
    人分四品
    手把手教你装饰vs2005项目上如何添加右键菜单
    递归算法学习系列之三(快速排序)
    ip地址与数字相互转换的sql函数
    递归算法学习系列之寻找第K大
    WSE 3.0 文档翻译:什么时候使用WSE 3.0
  • 原文地址:https://www.cnblogs.com/zcm123/p/6692510.html
Copyright © 2020-2023  润新知