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. Copy/paste of generic collection

Copy/paste of generic collection

Scheduled Pinned Locked Moved C#
csharpcomjsonhelpquestion
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.
  • G Offline
    G Offline
    Gone 0
    wrote on last edited by
    #1

    My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types? All entries in the CollectionBase are, of course, ISerializable and the binary serialization to file fully works. The collection is, to sketch the context, a set of shapes and connections of a diagram (application), see the Netron project[^] for details. Thank you so much for you help. :rose: The copy operation:

    CollectionBase<IDiagramEntity> copy = MyDeepCopyOfTheCollectionViaSerialization();
    DataFormats.Format format =
    DataFormats.GetFormat(typeof(CollectionBase<IDiagramEntity> ).FullName);
    IDataObject dataObject = new DataObject();
    dataObject.SetData(format.Name, false, copy);
    Clipboard.SetDataObject(dataObject, false);

    where the MyDeepCopyOfTheCollectionViaSerialization() is :

    CollectionBase<T> newobj = null;
    MemoryStream stream = new MemoryStream();
    GenericFormatter<BinaryFormatter> f = new
    GenericFormatter<BinaryFormatter>();
    f.Serialize(stream, this);
    stream.Seek(0, SeekOrigin.Begin);
    newobj = f.Deserialize<CollectionBase<T>>(stream);
    stream.Close();
    return newobj;

    And finally the Paste operation is:

    IDataObject data = Clipboard.GetDataObject();
    string format = typeof(CollectionBase<IDiagramEntity> ).FullName;
    if (data.GetDataPresent(format))
    {
    //data.GetData(format) is a MemoryStream rather than a generic collection! X|
    }

    I thought about it,
    I stood up
    and I did it.
    The Netron Project

    -- modified at 10:59 Saturday 18th March, 2006

    L 1 Reply Last reply
    0
    • G Gone 0

      My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types? All entries in the CollectionBase are, of course, ISerializable and the binary serialization to file fully works. The collection is, to sketch the context, a set of shapes and connections of a diagram (application), see the Netron project[^] for details. Thank you so much for you help. :rose: The copy operation:

      CollectionBase<IDiagramEntity> copy = MyDeepCopyOfTheCollectionViaSerialization();
      DataFormats.Format format =
      DataFormats.GetFormat(typeof(CollectionBase<IDiagramEntity> ).FullName);
      IDataObject dataObject = new DataObject();
      dataObject.SetData(format.Name, false, copy);
      Clipboard.SetDataObject(dataObject, false);

      where the MyDeepCopyOfTheCollectionViaSerialization() is :

      CollectionBase<T> newobj = null;
      MemoryStream stream = new MemoryStream();
      GenericFormatter<BinaryFormatter> f = new
      GenericFormatter<BinaryFormatter>();
      f.Serialize(stream, this);
      stream.Seek(0, SeekOrigin.Begin);
      newobj = f.Deserialize<CollectionBase<T>>(stream);
      stream.Close();
      return newobj;

      And finally the Paste operation is:

      IDataObject data = Clipboard.GetDataObject();
      string format = typeof(CollectionBase<IDiagramEntity> ).FullName;
      if (data.GetDataPresent(format))
      {
      //data.GetData(format) is a MemoryStream rather than a generic collection! X|
      }

      I thought about it,
      I stood up
      and I did it.
      The Netron Project

      -- modified at 10:59 Saturday 18th March, 2006

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      NetronProject wrote:

      f.Serialize(stream, this);stream.Seek(0, SeekOrigin.Begin);newobj = f.Deserialize>(stream);

      What on earth you doing there? Why not pass the just generated memorystream directly?

      xacc.ide-0.1.3.2

      G 1 Reply Last reply
      0
      • L leppie

        NetronProject wrote:

        f.Serialize(stream, this);stream.Seek(0, SeekOrigin.Begin);newobj = f.Deserialize>(stream);

        What on earth you doing there? Why not pass the just generated memorystream directly?

        xacc.ide-0.1.3.2

        G Offline
        G Offline
        Gone 0
        wrote on last edited by
        #3

        Ah! Interesting, though unorthodox idea. The logic is as follows; I first make a deep copy of the collection such that it's detached from the context. Since it's a copy of the collection is seemed logical to implement a 'Copy()' method. Of course, your suggestion is quite an interesting short-cut and I tried it out but...a Stream instance is not serializable and the pasting results in a 'null' value. On the other hand, an Image object is a kind of stream as well, no? :doh: The Clipboard has never been my favorite. Thanks a bunch for the idea! I have posted the question on Google, MSDN forums, here and you're the only one who has answered. :)

        I thought about it,
        I stood up
        and I did it.
        The Netron Project

        L 1 Reply Last reply
        0
        • G Gone 0

          Ah! Interesting, though unorthodox idea. The logic is as follows; I first make a deep copy of the collection such that it's detached from the context. Since it's a copy of the collection is seemed logical to implement a 'Copy()' method. Of course, your suggestion is quite an interesting short-cut and I tried it out but...a Stream instance is not serializable and the pasting results in a 'null' value. On the other hand, an Image object is a kind of stream as well, no? :doh: The Clipboard has never been my favorite. Thanks a bunch for the idea! I have posted the question on Google, MSDN forums, here and you're the only one who has answered. :)

          I thought about it,
          I stood up
          and I did it.
          The Netron Project

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          I have tried something similar, and as long as you remain with an application, rather use a static hashtable and map objects to GUID's or something. In my case I want to keep the proper uncloned references. HTH :)

          xacc.ide-0.1.3.2

          G 1 Reply Last reply
          0
          • L leppie

            I have tried something similar, and as long as you remain with an application, rather use a static hashtable and map objects to GUID's or something. In my case I want to keep the proper uncloned references. HTH :)

            xacc.ide-0.1.3.2

            G Offline
            G Offline
            Gone 0
            wrote on last edited by
            #5

            You know what...it's working. It seems things like

            stream.Seek(0, SeekOrigin.Begin);

            are not a good idea. Thanks! By the way, your XACC project is really impressive. :omg: Kind, F.

            I thought about it,
            I stood up
            and I did it.
            The Netron Project

            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