6

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.

3
  • Fun problem :) String and string are the same thing. I would think that it is more likely different versions of .Net and that serialization looks a bit different now Commented May 8, 2015 at 18:41
  • Just noticed that file1.bin is a public class where file2.bin is not. Could that be the issue? Commented May 8, 2015 at 19:01
  • so is the problem now more along the lines that you cannot serialize the class at all? Commented May 10, 2015 at 20:24

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.