Object to Memory Stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Evan Camilleri

    #1

    Object to Memory Stream


    How can I trasform (as fast as possible) an object to a binary memory
    stream?

    Evan


  • Stanimir Stoyanov

    #2
    Re: Object to Memory Stream

    "Evan Camilleri" <evan@holisticr d.com.nospamwro te in message
    news:eeVmw9%236 HHA.5424@TK2MSF TNGP02.phx.gbl. ..
    >
    How can I trasform (as fast as possible) an object to a binary memory
    stream?
    >
    Evan
    >
    Is the object a structure or of a particular class? If a class instance, is
    it your own implementation?

    Best Regards,
    Stanimir Stoyanov
    www.stoyanoff.info | www.aeroxp.org

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Object to Memory Stream

      Hi,
      "Evan Camilleri" <evan@holisticr d.com.nospamwro te in message
      news:eeVmw9%236 HHA.5424@TK2MSF TNGP02.phx.gbl. ..
      >
      How can I trasform (as fast as possible) an object to a binary memory
      stream?
      You can use Serialization, in case that the object is serializable. In any
      case it will consist in a number of calls to BitConverter.Ge tBytes() method.


      Comment

      • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

        #4
        RE: Object to Memory Stream

        Use the BinaryFormatter class and its Serialize method.
        -- Peter
        Recursion: see Recursion
        site: http://www.eggheadcafe.com
        unBlog: http://petesbloggerama.blogspot.com
        BlogMetaFinder: http://www.blogmetafinder.com



        "Evan Camilleri" wrote:
        >
        How can I trasform (as fast as possible) an object to a binary memory
        stream?
        >
        Evan
        >
        >
        >

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: Object to Memory Stream

          Evan Camilleri wrote:
          How can I trasform (as fast as possible) an object to a binary memory
          stream?
          Simple code:

          public static byte[] Object2ByteArra y(object o)
          {
          MemoryStream ms = new MemoryStream();
          BinaryFormatter bf = new BinaryFormatter ();
          bf.Serialize(ms , o);
          return ms.ToArray();
          }
          public static object ByteArray2Objec t(byte[] b)
          {
          MemoryStream ms = new MemoryStream(b) ;
          BinaryFormatter bf = new BinaryFormatter ();
          ms.Position = 0;
          return bf.Deserialize( ms);
          }

          Of if you like generics:

          public class Ser<T>
          {
          public static byte[] Object2ByteArra y(T o)
          {
          MemoryStream ms = new MemoryStream();
          BinaryFormatter bf = new BinaryFormatter ();
          bf.Serialize(ms , o);
          return ms.ToArray();
          }
          public static T ByteArray2Objec t(byte[] b)
          {
          MemoryStream ms = new MemoryStream(b) ;
          BinaryFormatter bf = new BinaryFormatter ();
          ms.Position = 0;
          return (T)bf.Deseriali ze(ms);
          }
          }

          Arne

          Comment

          Working...