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 に答える