By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

serializing non-serializable data members

P: n/a
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

Sep 26 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 25 Sep 2006 22:47:42 -0700, "dani k" <da*********@gmail.comwrote:
>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
Sep 26 '06 #2

P: n/a

dani k wrote:
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 !

Sep 26 '06 #3

P: n/a
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

--
Dave Sexton

"dani k" <da*********@gmail.comwrote in message news:11**********************@i3g2000cwc.googlegro ups.com...
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

Sep 26 '06 #4

P: n/a

Otis Mukinfus wrote:
On 25 Sep 2006 22:47:42 -0700, "dani k" <da*********@gmail.comwrote:
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
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
}

Sep 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.