Click to See Complete Forum and Search --> : Object serialization


simluk
March 7th, 2006, 02:54 AM
Hi,

i have some problems saving objects to binary file using serialization. Maybe i will just give some source code from my project and then try to explain the problem.

[Serializable()]
public class ObjectRefId : ISerializable
{
public ObjectRefId(SerializationInfo objSerializationInfo, StreamingContext objStreamingContext)
{
//Retrieve the data of the fields from the SerializationInfo class and
//set the values into the respective fields.
}

[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo objSerializationInfo, StreamingContext objStreamingContext)
{
//Add the fields to be saved into the SerializationInfo class.
//Provide a name for each field being saved.
}
}

public int LoadFromStorage()
{
String sName = "My3rdPartyStream";
try
{
object obj = document.IGet3rdPartyStorage(sName, false);
if (obj != null)
{
UCOMIStream ucomiStr = (UCOMIStream)obj;
ComStream comStream = new ComStream(ref ucomiStr);
comStream.Seek(0, SeekOrigin.Begin);

BinaryFormatter formatter = new BinaryFormatter();
comStream.Seek(0, SeekOrigin.Begin);
objRefId = (ObjectRefId)formatter.Deserialize(comStream);
comStream.Close();
}
}
catch(Exception ex)
{
iSwApp.SendMsgToUser2(ex.Message, (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
}

document.IRelease3rdPartyStorage(sName);

return 0;
}

public int SaveToStorage()
{
String sName = "My3rdPartyStream";
try
{
object obj = document.IGet3rdPartyStorage(sName, true);
if (obj != null)
{
UCOMIStream ucomiStr = (UCOMIStream)obj;
ComStream comStream = new ComStream(ref ucomiStr);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(comStream, objRefId);
comStream.Close();
}
}
catch(Exception ex)
{
iSwApp.SendMsgToUser2(ex.Message, (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
}

document.IRelease3rdPartyStorage(sName);

return 0;
}
}



I am developing addin application and i get notifications from host application when it saves and loads its file. I can use this notification to save my own objects within the host application files. If i try to save one of the base objects (like Hashtable) that implements ISeriazible interface, then everything works just fine. If i try to use my own objects that also implements ISeriazible interface, then i get an exception on Deserialize (BinaryFormatter) method. The message text is smth like:
"Cannot find the assembly MyAssemblyTest, Version.. "
At first i thougt that this could happen because my assembly is not "strog named", but i dont really understand how to make it "strong named" assembly..
Any help would be appreciated

hansipet
March 7th, 2006, 03:26 AM
I don't know why you get this exception. It is never happened to me. But I'm sure that you don't need a strong named assembly (I don't have too a strong assembly and it works also...

Regards
Hansjörg

hansipet
March 7th, 2006, 03:27 AM
Have you tested already to write it to your own file?
regards
Hansjörg

simluk
March 7th, 2006, 06:20 AM
Have you tested already to write it to your own file?
regards
Hansjörg

yep, tryed that already.. same exception is thrown
maybe my assembly info is incorrect - something with versioning???

hansipet
March 7th, 2006, 07:01 AM
If it is not a to big work, test to make a new project and insert there the relevant files. Maybe this helps...
Regards
Hansjörg

simluk
March 7th, 2006, 08:30 AM
If it is not a to big work, test to make a new project and insert there the relevant files. Maybe this helps...
Regards
Hansjörg

I have created test console application, where i tryed to serialize that object. Everything worked fine. Hmm..
Maybe these methods could cause problems in my addin (the code was generated by addin wizard)

[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
...
}

[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type t)
{
...
}


i wonder, what these registration functions do.. ??

hansipet
March 8th, 2006, 01:34 AM
Maybe you can test to add to this methods

[field:NonSerializedAttribute()]

or
[NonSerializedAttribute]

I'm not sure if this helps...if not the only way is to implement the ISerializable attribute

Regards
Hansjörg