简体   繁体   English

python打开一个序列化的C#文件

[英]python open a serialized C# file

I'm having an issue getting data out of ac# array serialized class with python. 我在使用python从ac#array序列化类中获取数据时遇到问题。 I have two files containing the classes. 我有两个包含类的文件。 The first I am able to loop though the array and grab the public variables. 第一个我能够遍历数组并获取公共变量。 However in the second file I see the class but am Unable to access any of the variables. 但是在第二个文件中我看到了类但是无法访问任何变量。 It's been 10+ years since I used C# and have been beating my head against the computer. 自从我使用C#以来,已经超过10年了,并且一直在打击电脑。 The only difference I can see is file1.bin uses String where file2.bin uses string. 我能看到的唯一区别是file1.bin使用String,其中file2.bin使用字符串。 Any pointers would be mot helpful. 任何指针都会很有帮助。

ironpython used to read .bin files. ironpython用于读取.bin文件。

from System.Runtime.Serialization.Formatters.Binary import BinaryFormatter
from System.IO import FileStream, FileMode, FileAccess, FileShare
from System.Collections.Generic import *

def read(name):
    bformatter = BinaryFormatter()
    file_stream = FileStream(name, FileMode.Open, FileAccess.Read,         FileShare.Read)
    res = bformatter.Deserialize(file_stream)
    file_stream.Close()
    return res

def write(name,data):
    bformatter = BinaryFormatter()
    stream = FileStream(name, FileMode.Create, FileAccess.ReadWrite,   FileShare.ReadWrite)
    bformatter.Serialize(stream, data)
    stream.Close()

res = read('fiel2.bin')
for space in res:
   print dir(space)

File1.bin - (simplified) array of Resident - Can Access data File1.bin - (简化)Resident数组 - 可以访问数据

namespace RanchoCSharp
{
    [Serializable]
    public class Resident
    {
        public Resident()
        {
        }
        public Resident(String fName, String lName)
        {
            firstN = fName;
            lastN = lName;
        }
        //Invoice info
        public String firstN;
        public String lastN;
    }
}

file2.bin (simplified) Array of Resident Info Can't access data file2.bin(简化)Resident Info数组无法访问数据

namespace Rancho_Resident
{
    [Serializable]
    class ResidentInfo
    {
       public ResidentInfo()
        {
        }
        public string unit;
        public string space;
    }
}

update 更新

After looking closer it appears that one class is public and the other is internal. 仔细看后,似乎一个班级是公开的,另一个班级是内部的。 However, I'm not sure how to access the internal class. 但是,我不确定如何访问内部类。

By default, internal members are invisible to external assemblies including IronPython, but you can change that. 默认情况下,内部成员对外部程序集(包括IronPython)不可见,但您可以更改它。

If you are running ipy.exe , start it by: 如果您正在运行ipy.exe ,请通过以下方式启动它:

ipy.exe -X:PrivateBinding

If you are hosting the IronPython scripting engine, then add an option: 如果您正在托管IronPython脚本引擎,请添加一个选项:

IDictionary<string, object> options = new Dictionary<string, object>();

options.Add("PrivateBinding", true);

ScriptEngine engine = IronPython.Hosting.Python.CreateEngine(options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM