CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2006
    Posts
    7

    Object serialization

    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.
    Code:
       [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

  2. #2
    Join Date
    Dec 2005
    Posts
    282

    Re: Object serialization

    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

  3. #3
    Join Date
    Dec 2005
    Posts
    282

    Re: Object serialization

    Have you tested already to write it to your own file?
    regards
    Hansjörg

  4. #4
    Join Date
    Mar 2006
    Posts
    7

    Re: Object serialization

    Quote Originally Posted by hansipet
    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???

  5. #5
    Join Date
    Dec 2005
    Posts
    282

    Re: Object serialization

    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

  6. #6
    Join Date
    Mar 2006
    Posts
    7

    Re: Object serialization

    Quote Originally Posted by hansipet
    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)
    Code:
     		[ComRegisterFunctionAttribute]
    		public static void RegisterFunction(Type t)
    		{
                     ...
                    }
    
    		[ComUnregisterFunctionAttribute]
    		public static void UnregisterFunction(Type t)
    		{
                     ...
                    }
    i wonder, what these registration functions do.. ??

  7. #7
    Join Date
    Dec 2005
    Posts
    282

    Re: Object serialization

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured