使用C#从IronPython源代码获取字符串

时间:2023-01-18 23:58:01

The book of IronPython In Action has the following code to read python script into a string. (Chapter 15.2)

IronPython In Action一书中有以下代码将python脚本读入字符串。 (第15.2章)

static string GetSourceCode(string pythonFileName)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream(pythonFileName);
    StreamReader textStreamReader = new StreamReader(stream);
    return textStreamReader.ReadToEnd();
}

It reads BasicEmbedding.source_code.py to a string. I just copied to my code, but I got the following error. (Just running from example code is OK)

它将BasicEmbedding.source_code.py读取为字符串。我只是复制到我的代码,但我得到以下错误。 (刚从示例代码运行就可以了)

Unhandled Exception: System.ArgumentNullException: Argument cannot be null.
Parameter name: stream
  at System.IO.StreamReader.Initialize (System.IO.Stream stream, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00000] in :0 
  at System.IO.StreamReader..ctor (System.IO.Stream stream, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00000] in :0 
  at System.IO.StreamReader..ctor (System.IO.Stream stream) [0x00000] in :0 
  at (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (System.IO.Stream)
  at BasicEmbedding.Program.GetSourceCode (System.String pythonFileName) [0x00000] in :0 
  at BasicEmbedding.Program.Main () [0x00000] in :0 

I think I can implement the same function as follows, which works OK.

我想我可以实现如下相同的功能,它可以正常工作。

static string GetSourceCode(string pythonFileName)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    string path = assembly.Location;
    string rootDir = Directory.GetParent(path).FullName;
    string pythonScript = Path.Combine(rootDir, pythonFileName);

    StreamReader textStreamReader = File.OpenText(pythonScript);

    return textStreamReader.ReadToEnd();
}

Question

  • For the original code, what's for the "assembly.GetManifestResourceStream()" function, and why do I get the error?
  • 对于原始代码,什么是“assembly.GetManifestResourceStream()”函数,为什么我会得到错误?
  • Is my new code the same as the old code in terms of execution result?
  • 在执行结果方面,我的新代码是否与旧代码相同?

1 个解决方案

#1


3  

  • For the original code, what's for the "assembly.GetManifestResourceStream()" function, and why do I get the error? : It looks for an embedded resource compiled into your application with the given name. Most likely, you are not adding a resource with that name.

    对于原始代码,什么是“assembly.GetManifestResourceStream()”函数,为什么我会得到错误? :它查找使用给定名称编译到应用程序中的嵌入式资源。最有可能的是,您没有添加具有该名称的资源。

  • Is my new code the same as the old code in terms of execution result? : No. Your reads a file from disk with the given name, in the same directory as the assembly. The original reads a resource from within the assembly.

    在执行结果方面,我的新代码是否与旧代码相同? :否。您从具有给定名称的磁盘中读取文件,与程序集位于同一目录中。原始文件从程序集中读取资源。

#1


3  

  • For the original code, what's for the "assembly.GetManifestResourceStream()" function, and why do I get the error? : It looks for an embedded resource compiled into your application with the given name. Most likely, you are not adding a resource with that name.

    对于原始代码,什么是“assembly.GetManifestResourceStream()”函数,为什么我会得到错误? :它查找使用给定名称编译到应用程序中的嵌入式资源。最有可能的是,您没有添加具有该名称的资源。

  • Is my new code the same as the old code in terms of execution result? : No. Your reads a file from disk with the given name, in the same directory as the assembly. The original reads a resource from within the assembly.

    在执行结果方面,我的新代码是否与旧代码相同? :否。您从具有给定名称的磁盘中读取文件,与程序集位于同一目录中。原始文件从程序集中读取资源。