How to deep clone the Class that contains DynamicObject in C#?

public class DynamicDictionary : System.Dynamic.DynamicObject
{
    Dictionary<string, object> dictionary = new Dictionary<string, object>();
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        string name = binder.Name;
        return dictionary.TryGetValue(name, out result);
    }
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;
        return true;
    }
    public override System.Collections.Generic.IEnumerable<string> GetDynamicMemberNames()
    {
        return this.dictionary?.Keys;
    }
}
public class SerializeExtensions
{
    public string Name { get; set; }
    public DynamicDictionary DynamicObj { get; set; }
}

Please consider the above like my class.

I have a List of SerializeExtensions collection. Now I have to deep clone this list of collections.

This has been properly cloned when I am using the below code.

JsonConvert.DeserializeObject<List<SerializeExtensions>>(JsonConvert.SerializeObject(collection));

This serialization belongs to Newtonsoft.Json. But I need to System.Text.Json for serialization.

So when I am using System.Text.Json serialization, the DynamicObject field has reset to empty. the below code has used.

JsonSerializer.Deserialize<List<SerializeExtensions>>(JsonSerializer.Serialize(collection));

After the serialization, I am getting the output like below

Name: "John"
DynamicObj: {}

Actually, the DynamicObj count is 4 in my case before serialization.

is there any possible to deep clone this kind of class?



from Recent Questions - Stack Overflow https://ift.tt/2WoFm3D
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation