Question

In order to support Copy/Paste in my application, I have to make all my objects serializable to be able to put them into the clipboard and get them again from there. It works as expected as long as there are no List<xy> objects that have to be serialized.

This is the first part on one object:

[Serializable]
public class Parameter : GObserverSubject, ISerializable, IObserver
{ 
    #region Attributes
    private static readonly ILog log = LogManager.GetLogger(typeof(Parameter));
    private int priority;
    private string name;
    private int id;
    private string description;
    private string comments;
    private List<Value> values;
    private Container myContainer;
    private bool changed;
    private int order;
    private bool noUpdate;
    #endregion
}

I also implemented those two methods:

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
    info.AddValue("priority", priority);
    info.AddValue("name", name);
    info.AddValue("id", id);
    info.AddValue("changed", true);
    info.AddValue("order", order);
    info.AddValue("values", values, values.GetType());
}

protected Parameter(SerializationInfo info, StreamingContext context)
{
    priority = info.GetInt32("priority");
    name = info.GetString("name");
    id = info.GetInt32("id");
    values = (List<Value>)info.GetValue("values", values.GetType());
    changed = info.GetBoolean("changed");
    order = info.GetInt32("order");
}

This is how I copy and paste stuff from my TreeView:

Parameter parameter = ((TreeNodeParameter)this.TestDesignTree.SelectedNode).getParameter();
Clipboard.SetDataObject(parameter);

IDataObject iData = Clipboard.GetDataObject();
Object copiedObject = iData.GetData(DataFormats.Serializable);
log.Info("type of selectednode is: " + this.TestDesignTree.SelectedNode.GetType());
log.Info("type of object in clipboard is: " + copiedObject.GetType());

The application crashes at copiedObject.GetType() with a NullReferenceException.

What am I doing wrong here?

Was it helpful?

Solution

Seems like the only problem was how I defined the type of the object:

values.GetType()

should be:

typeof(List<Value>);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top