在C中调用Python构造函数时出现Python异常错误#

2024-04-28 22:33:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我在代码的第1行调用python构造函数,得到以下异常:

{System.TypeInitializationException: The type initializer for 'IronPython.Compiler.Generation.OutputGenerator' threw an exception. ---> System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess, System.String, System.Security.Policy.Evidence)'. at IronPython.Compiler.Generation.AssemblyGen..ctor(String moduleName, String outDir, String outFile, Boolean emitDebugInfo, Boolean staticTypes, PortableExecutableKinds peKind, ImageFileMachine machine) at IronPython.Compiler.Generation.OutputGenerator.CreateNewSnippetAssembly(String name, Boolean emitDebugInfo) at IronPython.Compiler.Generation.OutputGenerator..cctor() --- End of inner exception stack trace --- at System.Runtime.CompilerServices.RuntimeHelpers._RunClassConstructor(RuntimeType type) at IronPython.Hosting.PythonEngine.Initialize(EngineOptions engineOptions) at TestPython.Program.Main(String[] args) in Program.cs:line 11}

代码如下:

using System;
using System.IO;
using IronPython.Hosting;

namespace TestPython
{
    class Program
    {
        static void Main(string[] args)
        {
            PythonEngine PE = new PythonEngine();

            string pythonFileName = "test.py";

            using (StreamWriter writetext = new StreamWriter(pythonFileName))
            {
                writetext.WriteLine("print('Hello, world from Python!')");
                writetext.WriteLine("input()");
            }

            PE.ExecuteFile(pythonFileName);

            Console.WriteLine("Hello World from C#!");
            while (Console.ReadKey().Key != ConsoleKey.Enter) { }
        }
    }
}

Tags: stringcompilerprogramsystematgenerationusingironpython