zoukankan      html  css  js  c++  java
  • C#动态编译代码

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

    public class Example
    {
        static void Main()
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameter = new CompilerParameters();
            parameter.ReferencedAssemblies.Add("System.dll");
            parameter.GenerateExecutable = false;
            parameter.GenerateInMemory = true;

            CompilerResults result = provider.CompileAssemblyFromSource(parameter,
                CreateCode("256*56*(145+56.0*254/345)"));//将你的式子放在这里
            if (result.Errors.Count > 0)
            {
                Console.WriteLine("动态编译出错了!");
            }
            else
            {
                Assembly assembly = result.CompiledAssembly;
                Type AType = assembly.GetType("ANameSpace.AClass");
                MethodInfo method = AType.GetMethod("AFunc");
                Console.WriteLine(method.Invoke(null, null));
            }
            Console.Read();
        }
        static string CreateCode( string para)
        {
            return "using System; namespace ANameSpace{static class AClass{public static object AFunc(){return "+para+";}}}";
        }
    }

  • 相关阅读:
    sql 存储过程
    Chrome系列 Failed to load resource: net::ERR_CACHE_MISS
    oledb 操作 excel
    [转]基于SQL脚本将数据库表及字段提取为C#中的类
    Ul li 竖排 菜单
    JS判断checkbox至少选择一项
    JS 字符串转日期格式 日期格式化字符串
    setInterval 实时驱动界面改变
    Let's Format Css Documents
    Web颜色搭配
  • 原文地址:https://www.cnblogs.com/pnljs/p/2222808.html
Copyright © 2011-2022 走看看