How to make a deep copy of an object not marked as serializable (in C #)?

I am trying to create a clipboard stack in C #. Clipboard data is stored in objects System.Windows.Forms.DataObject. I wanted to store each entry in the clipboard ( IDataObject) directly in the general list. Due to how Bitmaps is stored (it seems), I think that you need to do a deep copy first before adding it to the list.

I tried using binary serialization (see below) to create a deep copy, but since it is System.Windows.Forms.DataObjectnot marked as serializable, the serialization step fails. Any ideas?

public IDataObject GetClipboardData()
{
    MemoryStream memoryStream = new MemoryStream();
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    binaryFormatter.Serialize(memoryStream, Clipboard.GetDataObject());
    memoryStream.Position = 0;
    return (IDataObject) binaryFormatter.Deserialize(memoryStream);
}
+3
source share
3 answers

Serializable . , .net.

0

, , :

    public static class GhettoSerializer
    {
            // you could make this a factory method if your type
            // has a constructor that appeals to you (i.e. default 
            // parameterless constructor)
            public static void Initialize<T>(T instance, IDictionary<string, object> values)
            {
                    var props = typeof(T).GetProperties();

                    // my approach does nothing to handle rare properties with array indexers
                    var matches = props.Join(
                            values,
                            pi => pi.Name,
                            kvp => kvp.Key,
                            (property, kvp) =>
                                    new {
                                            Set = new Action<object,object,object[]>(property.SetValue), 
                                            kvp.Value
                                    }
                    );

                    foreach (var match in matches)
                            match.Set(instance, match.Value, null);
            }
            public static IDictionary<string, object> Serialize<T>(T instance)
            {
                    var props = typeof(T).GetProperties();

                    var ret = new Dictionary<string, object>();

                    foreach (var property in props)
                    {
                            if (!property.CanWrite || !property.CanRead)
                                    continue;
                            ret.Add(property.Name, property.GetValue(instance, null));
                    }

                    return ret;
            }
    }

, , .

+3

: DataContract Serializable .net

, , :

"... , , ?"

Reflection, , , .

. , . :

private void InspectRecursively(object input,
    Dictionary<object, bool> processedObjects)
{
  if ((input != null) && !processedObjects.ContainsKey(input))
  {
    processedObjects.Add(input, true);

    List<FieldInfo> fields = type.GetFields(BindingFlags.Instance |
        BindingFlags.Public | BindingFlags.NonPublic );
    foreach (FieldInfo field in fields)
    {
      object nextInput = field.GetValue(input);

      if (nextInput is System.Collections.IEnumerable)
      {
        System.Collections.IEnumerator enumerator = (nextInput as
            System.Collections.IEnumerable).GetEnumerator();

        while (enumerator.MoveNext())
        {
          InspectRecursively(enumerator.Current, processedObjects);
        }
      }
      else
      {
        InspectRecursively(nextInput, processedObjects);
      }
    }
  }
}

, - System.Runtime.Serialization.FormatterServices.GetUninitializedObject(Type type) ( ) . , - field.SetValue(input, output)

However, this implementation does not support registered event handlers, which are _ un _supported by deserialization. In addition, each object in the hierarchy will be split if its class constructor needs to initialize anything other than setting all the fields. The last point only works with serialization if the class has an appropriate implementation, for example. the method noted [OnDeserialized]implements ISerializable....

0
source

Source: https://habr.com/ru/post/1698267/


All Articles