Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serializing/Deserializing an object

Serializing/Deserializing an object

Scheduled Pinned Locked Moved C#
databasecsharpdata-structuresjsonperformance
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Helfdane
    wrote on last edited by
    #1

    ...and I fail at it :sigh: And somehow I can't get a working answer on Google. I want to store an in-memory generated assembly in the database for future use. I create this assembly from generated C#. For some reason, I can serialize the object, it generates a nice byte[] which is stored in the db. But when I want to retrieve the assembly for deserialisation to my object, I get an error like
    "{"Unable to find assembly '9-onzk69, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'."}" My serialization routine is:

    public byte[] Formula2ByteArray(Compiler.CompiledFormula formula)
    {
    byte[] result = null;

    try
    {
    System.IO.MemoryStream mStream = new System.IO.MemoryStream();
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

    formatter.Serialize(mStream, formula);
    
    result = mStream.ToArray();
    

    }
    catch (System.Exception ex)
    {
    throw ex;
    }

    return result;
    }

    And my deserialisation code is:

    public CompiledFormula LoadFormulaFromAssembly(byte[] array)
    {
    try
    {
    Compiler.CompiledFormula formula;

    using (System.IO.MemoryStream stream = new System.IO.MemoryStream(array))
    {
    	System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
    		new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    
    	formula = (CompiledFormula)formatter.Deserialize(stream);
    }
    
    return formula;
    

    }
    catch (System.Exception ex)
    {
    throw ex;
    }
    }

    Both the application which saves it into the db and the application which uses it contain the same libraries, so the objects-structures are known to both apps. De serialisation/deserialisation routines are in the same library/class working with the same object model. What am I missing? If I do something stupid, just tell me :)

    The consumer isn't a moron; she is your wife.

    H 1 Reply Last reply
    0
    • H Helfdane

      ...and I fail at it :sigh: And somehow I can't get a working answer on Google. I want to store an in-memory generated assembly in the database for future use. I create this assembly from generated C#. For some reason, I can serialize the object, it generates a nice byte[] which is stored in the db. But when I want to retrieve the assembly for deserialisation to my object, I get an error like
      "{"Unable to find assembly '9-onzk69, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'."}" My serialization routine is:

      public byte[] Formula2ByteArray(Compiler.CompiledFormula formula)
      {
      byte[] result = null;

      try
      {
      System.IO.MemoryStream mStream = new System.IO.MemoryStream();
      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
      new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

      formatter.Serialize(mStream, formula);
      
      result = mStream.ToArray();
      

      }
      catch (System.Exception ex)
      {
      throw ex;
      }

      return result;
      }

      And my deserialisation code is:

      public CompiledFormula LoadFormulaFromAssembly(byte[] array)
      {
      try
      {
      Compiler.CompiledFormula formula;

      using (System.IO.MemoryStream stream = new System.IO.MemoryStream(array))
      {
      	System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
      		new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
      
      	formula = (CompiledFormula)formatter.Deserialize(stream);
      }
      
      return formula;
      

      }
      catch (System.Exception ex)
      {
      throw ex;
      }
      }

      Both the application which saves it into the db and the application which uses it contain the same libraries, so the objects-structures are known to both apps. De serialisation/deserialisation routines are in the same library/class working with the same object model. What am I missing? If I do something stupid, just tell me :)

      The consumer isn't a moron; she is your wife.

      H Offline
      H Offline
      Hariharan T
      wrote on last edited by
      #2

      The answer might be stupid as well :) Just a thought, if you need to Serialize an object shouldnt that be marked with the attribute "[Serializable]"?

      Hariharan.T

      H 1 Reply Last reply
      0
      • H Hariharan T

        The answer might be stupid as well :) Just a thought, if you need to Serialize an object shouldnt that be marked with the attribute "[Serializable]"?

        Hariharan.T

        H Offline
        H Offline
        Helfdane
        wrote on last edited by
        #3

        The relevant classes are marked as Serializable, as otherwise the error would be notifying me which class is not serializable. This is not the case however, thus the serialize-attribute is set correctly.

        The consumer isn't a moron; she is your wife.

        H 1 Reply Last reply
        0
        • H Helfdane

          The relevant classes are marked as Serializable, as otherwise the error would be notifying me which class is not serializable. This is not the case however, thus the serialize-attribute is set correctly.

          The consumer isn't a moron; she is your wife.

          H Offline
          H Offline
          Hariharan T
          wrote on last edited by
          #4

          Can you modify your deserialization code a bit to narrow down where an exception is thrown? i mean seperate try catch for deserializing and one more for the type casting to find when it fails

          Hariharan.T

          H 1 Reply Last reply
          0
          • H Hariharan T

            Can you modify your deserialization code a bit to narrow down where an exception is thrown? i mean seperate try catch for deserializing and one more for the type casting to find when it fails

            Hariharan.T

            H Offline
            H Offline
            Helfdane
            wrote on last edited by
            #5

            The exception is thrown on the line: "formula = (CompiledFormula)formatter.Deserialize(stream);" This is the only deserializationcode I have, since I let the framework handle it. I'm suspecting the name '9-onzk69' is the name of the temporary file created by the codeprovider which compiles it, but I don't know for sure, as a system wide file search comes up empty. The stack trace of the exception is:

            at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
            at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
            at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
            at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
            at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
            at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
            at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
            at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
            at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
            at Compiler.CompilerSingleton.LoadFormuleFromAssembly(Byte[] array)

            The InnerException is null.

            The consumer isn't a moron; she is your wife.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups