Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python open a serialized C# file

Tags:

python

c#

I'm having an issue getting data out of a c# array serialized class with python. 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. The only difference I can see is file1.bin uses String where file2.bin uses string. Any pointers would be mot helpful.

ironpython used to read .bin files.

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

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

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.

like image 718
briarfox Avatar asked May 08 '15 18:05

briarfox


1 Answers

By default, internal members are invisible to external assemblies including IronPython, but you can change that.

If you are running ipy.exe, start it by:

ipy.exe -X:PrivateBinding

If you are hosting the IronPython scripting engine, then add an option:

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

options.Add("PrivateBinding", true);

ScriptEngine engine = IronPython.Hosting.Python.CreateEngine(options);
like image 126
muratgu Avatar answered Oct 25 '22 23:10

muratgu