Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CodeDom编译期间字符串输入中的动态关键字_C#_Dynamic_Compilation_Codedom_Runtime Compilation - Fatal编程技术网

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

C# CodeDom编译期间字符串输入中的动态关键字,c#,dynamic,compilation,codedom,runtime-compilation,C#,Dynamic,Compilation,Codedom,Runtime Compilation,为了将字符串转换为代码,我使用了CodeDom编译器。 我说的是在运行时编译C#代码 特别是我想用几个不同类型的参数进行运行时编译 using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Data; using System.Diagnostics; using Syst

为了将字符串转换为代码,我使用了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");
    }
而不是

     String2LineOfCommand("P[0].ToString() + P[1].ToString()", new [] { 2, 4});
       public static object Function1(params int[] P)
然后我将收到一条错误消息:

   An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll
   Additional information: Parameter count mismatch.
      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 MethodInfo CreateFunction(string function)  
当我试着打字的时候

       public static object Function1(params object[] P) 
      public static object Function(params dynamic[] P)  
而不是

     String2LineOfCommand("P[0].ToString() + P[1].ToString()", new [] { 2, 4});
       public static object Function1(params int[] P)
它向我显示一条错误消息:

   An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll
   Additional information: Parameter count mismatch.
      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 Function1(params object[] P) 
      public static object Function(params dynamic[] P)  
编译失败,并且没有关于错误的消息
我想我错过了一个推荐人
动态关键字可用于.net 4.0
我已经写过:

      providerOptions.Add("CompilerVersion", "v4.0");
但是没有结果


请帮帮我

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

代码,使其与动态(或对象,我看不出动态的意义):

使用系统;
使用System.CodeDom.Compiler;
使用System.Collections.Generic;
使用系统诊断;
运用系统反思;
使用Microsoft.CSharp;
公共课程
{
静态void Main()
{
string2line命令(“P[0].ToString()+P[1].ToString()”,2,4);
}
私有静态无效String2LineOfCommand(字符串表达式1,参数dynamic[]P)
{
MethodInfo function=CreateFunction(expresion1);
object result=function.Invoke(null,new[]{P});
Console.WriteLine(result.ToString());
Debug.WriteLine(result.ToString());
}
公共静态MethodInfo CreateFunction(字符串函数)
{
字符串代码1=@“
使用制度;
使用System.Collections.Generic;
运用系统反思;
运用系统动力学;
命名空间用户函数
{                
公共类参数函数
{                
公共静态对象函数1(动态[]P)
{
返回“+函数+@”;
}
}
}
";
var providerOptions=new Dictionary();
添加(“CompilerVersion”、“v4.0”);
CSharpCodeProvider provider=新的CSharpCodeProvider(providerOptions);
CompilerParameters cp=新的CompilerParameters();
cp.GenerateInMemory=true;
cp.TreatWarningsAsErrors=false;
添加(“System.dll”);
添加(“System.Core.dll”);
添加(“System.Windows.dll”);
添加(“System.Dynamic.dll”);
添加(“Microsoft.CSharp.dll”);
CompilerResults results=provider.compileAsemblyFromSource(cp,代码1);
Type parametricFunction1=results.CompiledAssembly.GetType(“UserFunctions.ParametricFunctions”);
返回参数Function1.GetMethod(“Function1”);
}
}

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

代码,使其与动态(或对象,我看不出动态的意义):

使用系统;
使用System.CodeDom.Compiler;
使用System.Collections.Generic;
使用系统诊断;
运用系统反思;
使用Microsoft.CSharp;
公共课程
{
静态void Main()
{
string2line命令(“P[0].ToString()+P[1].ToString()”,2,4);
}
私有静态无效String2LineOfCommand(字符串表达式1,参数dynamic[]P)
{
MethodInfo function=CreateFunction(expresion1);
object result=function.Invoke(null,new[]{P});
Console.WriteLine(result.ToString());
Debug.WriteLine(result.ToString());
}
公共静态MethodInfo CreateFunction(字符串函数)
{
字符串代码1=@“
使用制度;
使用System.Collections.Generic;
运用系统反思;
运用系统动力学;
命名空间用户函数
{                
公共类参数函数
{                
公共静态对象函数1(动态[]P)
{
返回“+函数+@”;
}
}
}
";
var providerOptions=new Dictionary();
添加(“CompilerVersion”、“v4.0”);
CSharpCodeProvider provider=新的CSharpCodeProvider(providerOptions);
CompilerParameters cp=新的CompilerParameters();
cp.GenerateInMemory=true;
cp.TreatWarningsAsErrors=false;
添加(“System.dll”);
添加(“System.Core.dll”);
添加(“System.Windows.dll”);
添加(“System.Dynamic.dll”);
添加(“Microsoft.CSharp.dll”);
CompilerResults results=provider.compileAsemblyFromSource(cp,代码1);
Type parametricFunction1=results.CompiledAssembly.GetType(“UserFunctions.ParametricFunctions”);
返回参数Function1.GetMethod(“Function1”);
}
}

再次感谢您。我想我找到了!当我编写动态关键字而不是int时出现了3个错误…我通过“results.errors.Count”得到了这3个错误是的,对于第一个问题,您需要添加cp.referencedAssemblys.add(“Microsoft.CSharp.dll”)!再次感谢你!但是参数呢?为什么它再次告诉我:“'System.Int32[]”不能