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

            

    a)         XML结构

    <?xml version="1.0" encoding="utf-8"?>

    <Root >

    <Function Type ="double" name ="GetCocThreshold">

    <ParameterList>

    <Parameter Type ="double" name = "Coc_Th_Idea," />

    <Parameter Type ="double" name = "Rsense," />

    <Parameter Type ="double" name = "Coc_Offset" />

    </ParameterList>

    <Steps>

    <Step Type ="double" Result ="Result" Sentence ="Coc_Th_Idea*Rsense/1000.0" />

    <Step Type ="double" Result ="Result1" Sentence ="((Result - Coc_Offset)/5.0)+4" />

    </Steps>

    </Function>

    </Root>

    b)  内存格式:

    using System;

    namespace ParseEx

    {                                             

    public class ParseExC                        

                 {      

    public static double GetCocThreshold(double Coc_Th_Idea,double Rsense,double Coc_Offset)

    {

    double Result=Coc_Th_Idea*Rsense/1000.0;

    double Result1=((Result - Coc_Offset)/5.0)+4;

    return Result1;

    }

    }

    }

    c)         例程

    static void Main(string[] args)

                        {

                                  StringBuilder codeBuilder = new StringBuilder();   

                                  string path = @"ExtensionFunctionLibrary.xml";

                                  string strCode = @" using System;                    

                                    namespace ParseEx                    

                                    {                        

                                        public class ParseExC                        

                                        {      

                                        ";

                                  codeBuilder.AppendLine(strCode);

                                  try

                                  {

                                            XElement rootNode = XElement.Load(path);

                                            IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("Function") select target;

                                            foreach (XElement node in targetNodes)

                                            {

                                        strCode = "public static ";

                                        codeBuilder.AppendLine(strCode);

                                        strCode = node.Attribute("Type").Value.ToString() +" "+ node.Attribute("name").Value.ToString()+"" + "(";

                                        IEnumerable<XElement> ParameterListNodes = from target in node.Descendants("ParameterList") select target;

                                        foreach (XElement ParameterList in ParameterListNodes)

                                        {

                                                  IEnumerable<XElement> ParameterNode = from target in ParameterList.Descendants("Parameter") select target;

                                                  foreach (XElement parameter in ParameterNode)

                                                  {

                                    strCode += parameter.Attribute("Type").Value.ToString() + " " + parameter.Attribute("name").Value.ToString() + "";

                                                  }

                                                  strCode += "){";

                          }

                        codeBuilder.AppendLine(strCode);

                        strCode = "";

                        IEnumerable<XElement> StepListNodes = from target in node.Descendants("Steps") select target;

                        foreach (XElement StepList in StepListNodes)

                        {

                            IEnumerable<XElement> StepNode = from target in StepList.Descendants("Step") select target;

                            foreach (XElement Step in StepNode)

                            {

                                strCode += Step.Attribute("Type").Value.ToString() + " " + Step.Attribute("Result").Value.ToString() + "=" + Step.Attribute("Sentence").Value.ToString() + ";"; ;

                            }

                            strCode += "return Result1;}";

                        }

                        strCode += "}}";

                        codeBuilder.AppendLine(strCode);

                        string str = codeBuilder.ToString();

                    }

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.ToString());

                }

                CodeDomProvider comp  = new CSharpCodeProvider();    

                CompilerParameters cp = new CompilerParameters();  

                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)            

                        {          

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

                            if (mi != null)                

                            {

                                double d = (double)mi.Invoke(null, new object[3]{1,2,3});

                                Console.Write(d.ToString());                

                            }            

                        }        

                    }    

                }

            }

    编译结果:3.4004.

    从而实现,函数可以在文档中定义,代码可以动态生成Function根据文档的描述,这样灵活性更高,可以用一段代码实现不同函数。可以应用于工程包切换。

  • 相关阅读:
    git获取远程路径
    qt 运行直接运行exe文件
    QML动画和过度
    学习网络编程的十个步骤
    vs中设置中断
    qt出现警告 Unescaped backslashes are deprecated!解决办法
    git把某此提交运用到某个分支上
    qml学习文档转载
    git远程分支的创建与推送
    qml实现折叠框
  • 原文地址:https://www.cnblogs.com/yiyi20120822/p/2784444.html
Copyright © 2011-2022 走看看