簡體   English   中英

python打開一個序列化的C#文件

[英]python open a serialized C# file

我在使用python從ac#array序列化類中獲取數據時遇到問題。 我有兩個包含類的文件。 第一個我能夠遍歷數組並獲取公共變量。 但是在第二個文件中我看到了類但是無法訪問任何變量。 自從我使用C#以來,已經超過10年了,並且一直在打擊電腦。 我能看到的唯一區別是file1.bin使用String,其中file2.bin使用字符串。 任何指針都會很有幫助。

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 - (簡化)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(簡化)Resident Info數組無法訪問數據

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

更新

仔細看后,似乎一個班級是公開的,另一個班級是內部的。 但是,我不確定如何訪問內部類。

默認情況下,內部成員對外部程序集(包括IronPython)不可見,但您可以更改它。

如果您正在運行ipy.exe ,請通過以下方式啟動它:

ipy.exe -X:PrivateBinding

如果您正在托管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