zoukankan      html  css  js  c++  java
  • 如何用C#动态编译、执行代码例程

             static void Main(string[] args)

            {

                string strCode = @" using System;                    

                                    namespace ParseEx                    

                                    {                        

                                        public class ParseExC                        

                                        {                            

                                            public static double GetValue()                                                    

                                           {

                                                return result();

                                            }

                                            private static double result()

                                            {

                                                return 1+5*8-(Math.Sin(12));

                                            }

                                         }          

                                    }";      

                CodeDomProvider comp  = new CSharpCodeProvider();    

                CompilerParameters cp = new CompilerParameters();  

                   

                StringBuilder codeBuilder = new StringBuilder();                

                codeBuilder.AppendLine(strCode);   

                cp.ReferencedAssemblies.Add("System.dll");    

                cp.GenerateExecutable = false;    

                cp.GenerateInMemory   = true;          

                CompilerResults cr = comp.CompileAssemblyFromSource(cp, codeBuilder.ToString());    

                if (cr.Errors.HasErrors)    

                {

                    Console.Write("Error!");

                }    

                else   

                {        

                    Assembly a = cr.CompiledAssembly;        

                    if (a != null)        

                    {            

                        Type t = a.GetType("ParseEx.ParseExC");            

                        if (t != null)            

                        {                          

                            // object mode = a.CreateInstance("Mode");                

                            MethodInfo mi = t.GetMethod("GetValue", BindingFlags.Static | BindingFlags.Public);                

                            if (mi != null)                

                            {

                                double d = (double)mi.Invoke(null, null);

                                Console.Write(d.ToString());                

                            }            

                        }        

                    }    

                }

            }

    测试结果:41.5365

  • 相关阅读:
    python模块添加
    Python 的列表排序
    python中文处理问题
    排序算法堆排序
    搜索二分搜索
    排序算法(随机)快速排序(递归)
    排序算法计数排序
    OO设计原则总结
    异常控制以及进程调度
    ubuntu12.04 alternate win7 双系统安装
  • 原文地址:https://www.cnblogs.com/yiyi20120822/p/2784431.html
Copyright © 2011-2022 走看看