【发布时间】:2010-10-11 17:34:29
【问题描述】:
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();
}
它将 BasicEmbedding.source_code.py 读取为一个字符串。我刚刚复制到我的代码,但出现以下错误。 (只需从示例代码运行即可)
未处理的异常:System.ArgumentNullException:参数不能为空。 参数名称:流 在 System.IO.StreamReader.Initialize(System.IO.Stream 流,System.Text.Encoding 编码,布尔检测EncodingFromByteOrderMarks,Int32 bufferSize)[0x00000] 在:0 在 System.IO.StreamReader..ctor (System.IO.Stream 流,System.Text.Encoding 编码,布尔检测EncodingFromByteOrderMarks,Int32 bufferSize)[0x00000] 在:0 在 System.IO.StreamReader..ctor (System.IO.Stream 流) [0x00000] in :0 在 (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (System.IO.Stream) 在 BasicEmbedding.Program.GetSourceCode (System.String pythonFileName) [0x00000] in :0 在 BasicEmbedding.Program.Main () [0x00000] in :0我想我可以实现如下相同的功能,它工作正常。
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();
}
问题
- 对于原始代码,“assembly.GetManifestResourceStream()”函数是什么,为什么会出现错误?
- 我的新代码和旧代码的执行结果一样吗?
【问题讨论】:
标签: c# .net ironpython