문제

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?

도움이 되었습니까?

해결책

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

values.GetType()

should be:

typeof(List<Value>);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top