Problem with accessint to private fields :)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi frends.
I have very interesting problem. It's solution possibly not exist.
Take a look.

I need to create a method that can serialize any generic C# class (even when
it's not implement ISerializable). The idea is simple: 'Store all fields to
disk and later restore that class by reading data'. The problem is that most
of classes conatins private fields wich cann't be accessed duraring process
of serialization/deserialization.

Look's like that for sloving that problem I have something like to crack CLR.
If anyone know some way to do that plz tell.
 
No it's not answer to my question. Serializer in that example is similar to
one that I develop but it just skip private fields and doesn't store it. It's
not good for me...
 
Lacky said:
Hi frends.
I have very interesting problem. It's solution possibly not exist.
Take a look.

I need to create a method that can serialize any generic C# class (even when
it's not implement ISerializable). The idea is simple: 'Store all fields to
disk and later restore that class by reading data'. The problem is that most
of classes conatins private fields wich cann't be accessed duraring process
of serialization/deserialization.
You could probably access these fields using reflection and then
serialize the results in some custom way.

JB
 
Offcourse I have to use Reflections to get structure of the class that I
going to serialize. But my problem IS "I cann't read data from private fields
of generic class"
 
Lacky said:
Offcourse I have to use Reflections to get structure of the class that I
going to serialize. But my problem IS "I cann't read data from private fields
of generic class"
Why not?
You can reflect the private values.

JB
 
Hello

Here is code of a class I used for serializing objects not marked
as serializable. I used it to serialize automaticaly generated objects.

using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Remoting;

[Serializable]
public class ObjectSerializer : ISerializable
{
protected const string ASSEMBLY_KEY = "assembly";
protected const string TYPE_NAME_KEY = "type_name";
protected const string FIELD_KEY_PREFIX = "field_";
protected const string NULL_VALUE_KEY = "null";

protected object obj;

public object Object
{
get { return obj; }
}

public ObjectSerializer(object obj)
{
this.obj = obj;
}

protected ObjectSerializer(SerializationInfo si, StreamingContext sc)
{
bool nullValue = si.GetBoolean(NULL_VALUE_KEY);

if(nullValue)
obj = null;
else
{
string assemblyName = si.GetString(ASSEMBLY_KEY);
string typeName = si.GetString(TYPE_NAME_KEY);
obj = Activator.CreateInstance(assemblyName, typeName).Unwrap();

Type type = obj.GetType();

FieldInfo[] fia = type.GetFields(
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);

foreach(FieldInfo fi in fia)
{
object val;

if(fi.FieldType.IsSerializable)
val = si.GetValue(FIELD_KEY_PREFIX + fi.Name, fi.FieldType);
else
{
ObjectSerializer os = (ObjectSerializer)si.GetValue(
FIELD_KEY_PREFIX + fi.Name, typeof(ObjectSerializer));
val = os.Object;
}

fi.SetValue(obj, val);
}
}
}

public void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue(NULL_VALUE_KEY, (obj == null));

if(obj != null)
{
Type type = obj.GetType();

info.AddValue(ASSEMBLY_KEY, type.Assembly.GetName().Name);
info.AddValue(TYPE_NAME_KEY, type.FullName);

FieldInfo[] fia = type.GetFields(
BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public);

foreach(FieldInfo fi in fia)
{
object toSerialize;

if(fi.FieldType.IsSerializable)
toSerialize = fi.GetValue(obj);
else
toSerialize = new ObjectSerializer(fi.GetValue(obj));

info.AddValue(FIELD_KEY_PREFIX + fi.Name, toSerialize);
}
}
}
}


// serialize
ObjectSerializer os = new ObjectSerializer(ofw.GetCube());
ms = new MemoryStream();
formatter.Serialize(ms, os);

// deserialize
ms = new MemoryStream(ba);
ObjectSerializer osCube = (ObjectSerializer)formatter.Deserialize(ms);
Cube cube = (Cube)osCube.Object;

Aleksandar
 
Back
Top