CodeDom 编译期间字符串输入中的动态关键字

dynamic keyword in string input during CodeDom compilation

为了将字符串转换为代码,我使用了 CodeDom 编译器。
我说的是在运行时编译 C# 代码!
特别是我想用几个类型可以不同的参数进行运行时编译。

    using System;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Dynamic;
    using System.Net;
    using System.Reflection;
    using Microsoft.CSharp;

    static void Main()
    {
         String2LineOfCommand("P[0].ToString() + P[1].ToString()", new [] { 2, 4});
    }

    private void String2LineOfCommand(string expresion1, params object[] P)
    {
        MethodInfo function = CreateFunction(expresion1);
        object result = function.Invoke(null, P);
        Console.WriteLine(result.ToString());
    }

    public static MethodInfo CreateFunction(string function)
    {
        string code1 = @"
                        using System;
                        using System.Collections.Generic;
                        using System.Reflection;
                        using System.Dynamic;

                        namespace UserFunctions
                        {                
                            public class ParametricFunctions
                            {                
                                  public static object Function1(params int[] P)
                                  {
                                       return " +  function + @";
                                  }
                             }
                         }
                       ";

        var providerOptions = new Dictionary<string, string>();
        providerOptions.Add("CompilerVersion", "v4.0");

        CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
        CompilerParameters cp = new CompilerParameters();
        cp.GenerateInMemory = true;
        cp.TreatWarningsAsErrors = false;
        cp.ReferencedAssemblies.Add("System.dll");
        cp.ReferencedAssemblies.Add("System.Core.dll");
        cp.ReferencedAssemblies.Add("System.Windows.dll");
        cp.ReferencedAssemblies.Add("System.Dynamic.dll");

        CompilerResults results = provider.CompileAssemblyFromSource(cp, code1);

        Type parametricFunction1 = results.CompiledAssembly.GetType("UserFunctions.ParametricFunctions");
        return parametricFunction1.GetMethod("Function1");
    }

这段代码工作得很好!
但是正如我之前告诉你的,我不是在谈论 int 类型参数。
我说的是运行时不同情况下不同类型的输入参数!

如果我会输入

     String2LineOfCommand("P[0].ToString() + P[1].ToString()", 2, 4);

而不是

     String2LineOfCommand("P[0].ToString() + P[1].ToString()", new [] { 2, 4});

然后我会得到一条错误信息:

   An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll
   Additional information: Parameter count mismatch.

请告诉我为什么?

也看看里面...

       public static MethodInfo CreateFunction(string function)  

当我尝试输入

       public static object Function1(params object[] P) 

而不是

       public static object Function1(params int[] P)

它向我显示一条错误消息:

      An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
      Additional information: Object of type 'System.Int32[]' cannot be converted to type 'System.Object[]'.  

但是当我尝试输入

      public static object Function(params dynamic[] P)  

编译失败,没有关于错误的信息!
我想我缺少参考!
动态关键字可用于 .net 4.0
我已经写了:

      providerOptions.Add("CompilerVersion", "v4.0");

但是没有结果!

请帮助我!

CompilerResults class 有错误 属性。编译失败的错误在那里。

代码使其与动态(或对象,我在这里看不到动态的意义)一起工作:

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Microsoft.CSharp;

public class Program
{

    static void Main()
    {
        String2LineOfCommand("P[0].ToString() + P[1].ToString()", 2, 4);
    }

    private static void String2LineOfCommand(string expresion1, params dynamic[] P)
    {
        MethodInfo function = CreateFunction(expresion1);
        object result = function.Invoke(null, new[] { P });
        Console.WriteLine(result.ToString());
        Debug.WriteLine(result.ToString());
    }

    public static MethodInfo CreateFunction(string function)
    {
        string code1 = @"
                        using System;
                        using System.Collections.Generic;
                        using System.Reflection;
                        using System.Dynamic;

                        namespace UserFunctions
                        {                
                            public class ParametricFunctions
                            {                
                                  public static object Function1(dynamic[] P)
                                  {
                                       return " + function + @";
                                  }
                             }
                         }
                       ";

        var providerOptions = new Dictionary<string, string>();
        providerOptions.Add("CompilerVersion", "v4.0");

        CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
        CompilerParameters cp = new CompilerParameters();
        cp.GenerateInMemory = true;
        cp.TreatWarningsAsErrors = false;
        cp.ReferencedAssemblies.Add("System.dll");
        cp.ReferencedAssemblies.Add("System.Core.dll");
        cp.ReferencedAssemblies.Add("System.Windows.dll");
        cp.ReferencedAssemblies.Add("System.Dynamic.dll");
        cp.ReferencedAssemblies.Add("Microsoft.CSharp.dll");

        CompilerResults results = provider.CompileAssemblyFromSource(cp, code1);

        Type parametricFunction1 = results.CompiledAssembly.GetType("UserFunctions.ParametricFunctions");
        return parametricFunction1.GetMethod("Function1");
    }
}