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根据文档的描述,这样灵活性更高,可以用一段代码实现不同函数。可以应用于工程包切换。