serializing non-serializable data members

D

dani k

How do I write my own serialization of a class that has
non-serializable data members?
this problem arose when I tried to serialize RSAParameters, for which
the private stuff is not serializable
 
O

Otis Mukinfus

How do I write my own serialization of a class that has
non-serializable data members?
this problem arose when I tried to serialize RSAParameters, for which
the private stuff is not serializable

I'm afraid you can't serialize the private fields of objects you don't have the
source for, but if the private fields are exposed as properties, you might be
able to inherit from the class and serialize those properties in the derived
class.

Did I misunderstand your question?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
C

coolCoder

dani said:
How do I write my own serialization of a class that has
non-serializable data members?
this problem arose when I tried to serialize RSAParameters, for which
the private stuff is not serializable

If your sole purpose is serializing all the members, you can create a
method which will save all the members of your class, may be it private
or public, to a binary or simple text file. May be you can put some
file name as parameter to this method.
Another method of your class will read some binary / text file and just
assign the value to all the members serialized from the first method.
This could be a solution for your problem.
These both methods will be public so that any instance of your class
gives access to these methods.
I think this should work for you.
It will be a tedious job once, but you can define your own format for
the file also in what format you save your data.


Hope i understood your question and am clear in my description.

Thanks !
 
D

Dave Sexton

Hi,

Implement ISerializable on your class. The formatter that you use for serialization will use your serialization code instead of
default serialization, which tries to serialize all members.

In your implementation of ISerializable.GetObjectData add the information that needs to be persisted. Since you can't add the
RSAParameters object to the SerializationInfo, only add the information that is required to reconstruct the RSAParameters object
during deserialization.

A constructor on your class that takes SerializationInfo and StreamingContext as arguments is required for deserialization. In this
constructor, read the persisted data from the SerializationInfo object and populate your object's fields. Read the RSAParameters
data that you persisted and construct a new instance.

HTH
 
D

dani k

Otis said:
I'm afraid you can't serialize the private fields of objects you don't have the
source for, but if the private fields are exposed as properties, you might be
able to inherit from the class and serialize those properties in the derived
class.

Did I misunderstand your question?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Yes, I am afraid I didn't state the problem very well. Thanks anyway.
Anyway, I figured it out: I included an RSAParameters object as a
member in a new class that implements the ISerializable interface, and
wrote my own serialization code.
It works!
Here is the code:

public class AllSerRSAParameters:ISerializable
{
private RSAParameters RSAKeyInfo;

public AllSerRSAParameters(RSAParameters rhs)
{
RSAKeyInfo = rhs;
}
public AllSerRSAParameters(SerializationInfo si,
StreamingContext context)
{
RSAKeyInfo.D = (Byte[]) si.GetValue( "D", typeof(Byte[]));
RSAKeyInfo.DP = (Byte[]) si.GetValue( "DP",
typeof(Byte[]));
RSAKeyInfo.DQ = (Byte[])si.GetValue("DQ", typeof(Byte[]));
RSAKeyInfo.Exponent = (Byte[])si.GetValue("Exponent",
typeof(Byte[]));
RSAKeyInfo.InverseQ = (Byte[])si.GetValue("InverseQ",
typeof(Byte[]));
RSAKeyInfo.Modulus = (Byte[])si.GetValue("Modulus",
typeof(Byte[]));
RSAKeyInfo.P = (Byte[])si.GetValue("P", typeof(Byte[]));
RSAKeyInfo.Q = (Byte[])si.GetValue("Q", typeof(Byte[]));
}

public RSAParameters GetRSAParameters()
{
return RSAKeyInfo;
}

#region ISerializable Members

[SecurityPermission(SecurityAction.Demand,
SerializationFormatter = true)]
public void GetObjectData(SerializationInfo si,
StreamingContext context)
{
si.AddValue("D", RSAKeyInfo.D, typeof(Byte[]));
si.AddValue("DP", RSAKeyInfo.DP, typeof(Byte[]));
si.AddValue("DQ", RSAKeyInfo.DQ, typeof(Byte[]));
si.AddValue("Exponent", RSAKeyInfo.Exponent,
typeof(Byte[]));
si.AddValue("InverseQ", RSAKeyInfo.InverseQ,
typeof(Byte[]));
si.AddValue("Modulus", RSAKeyInfo.Modulus, typeof(Byte[]));
si.AddValue("P", RSAKeyInfo.P, typeof(Byte[]));
si.AddValue("Q", RSAKeyInfo.Q, typeof(Byte[]));
}

#endregion
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top