1

我正在尝试从单声道的 C API 编译 C# 源代码。请在下面查看我的代码。

    //System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); 
    MonoClass* pCompilerParamsClass = mono_class_from_name (pSystemImage, "System.CodeDom.Compiler", "CompilerParameters"); 
    MonoObject* pCompilerParams = mono_object_new (pDomain, pCompilerParamsClass); 
    mono_runtime_object_init (pCompilerParams); 

    //parameters.GenerateExecutable = true; 
    //parameters.OutputAssembly = "Rules.dll"; 
    SetBoolProperty(pCompilerParams, pCompilerParamsClass, "System.CodeDom.Compiler.CompilerParameters:set_GenerateExecutable", true); 
    SetStringProperty(pCompilerParams, pCompilerParamsClass, "System.CodeDom.Compiler.CompilerParameters:set_OutputAssembly", "Rules.dll"); 

    //string sourceString = "class A {}" 
    MonoString* pSourceString = mono_string_new(pDomain, "class A {}"); 

    //CSharpCodeProvider codeProvider = new CSharpCodeProvider(); 
    MonoClass* pCodeProviderClass = mono_class_from_name (pSystemImage, "System.CodeDom.Compiler", "CodeDomProvider"); 
    MonoClass* pCSharpCodeProviderClass = mono_class_from_name (pSystemImage, "Microsoft.CSharp", "CSharpCodeProvider"); 
    MonoObject* pCodeProvider = mono_object_new (pDomain, pCSharpCodeProviderClass); 
    mono_runtime_object_init (pCodeProvider); 

    //ICodeCompiler icc = codeProvider.CreateCompiler(); 
    MonoMethodDesc* pMethodDescCreateCompiler = mono_method_desc_new("System.CodeDom.Compiler.CodeDomProvider:CreateCompiler()", false); 
    MonoMethod* pMethodCreateCompiler = mono_method_desc_search_in_class (pMethodDescCreateCompiler, pCSharpCodeProviderClass); 
    MonoObject* pIcc = mono_runtime_invoke(pMethodCreateCompiler, pCodeProvider, NULL, NULL);

    //icc.CompileAssemblyFromSource(parameters, source) 
    MonoMethodDesc* pMethodDescCompileAssemblyFromSource = mono_method_desc_new("System.CodeDom.Compiler.ICodeCompiler:CompileAssemblyFromSource()", false); 
    MonoMethod* pMethodCompileAssemblyFromSource = mono_method_desc_search_in_class (pMethodDescCreateCompiler, mono_object_get_class(pIcc)); 

    MonoArray* pParams = mono_array_new(pDomain, mono_get_object_class(), 2); 
    mono_array_set(pParams, MonoObject*, 0, pCompilerParams); 
    mono_array_set(pParams, MonoString*, 0, pSourceString); 

    mono_runtime_invoke_array(pMethodCompileAssemblyFromSource, pCodeProvider, NULL, NULL); 

此代码在 MonoObject* pIcc = mono_runtime_invoke(pMethodCreateCompiler, pCodeProvider, NULL, NULL); 处失败;线。

我究竟做错了什么?

4

0 回答 0