0

为了将字符串转换为代码,我使用了 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)  

编译失败并且没有关于错误的消息!
我想我缺少参考!
dynamic 关键字可用于 .net 4.0
我已经写过:

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

但是没有结果!

请帮我!

4

1 回答 1

2

CompilerResults 类有一个 Errors 属性。编译失败的错误在那里。

代码使其适用于动态(或对象,我在这里看不到动态的意义):

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");
    }
}
于 2016-03-10T13:03:18.437 回答