replaysMike / AnyClone

A CSharp library that can deep clone any object using only reflection.
MIT License
47 stars 7 forks source link

overriding json ignore #7

Closed touseefbsb closed 3 years ago

touseefbsb commented 5 years ago

I have some properties in my dto models which have json ignores tags on them bcz we dont need them in the db but we do need them on the client side. so that means when I created my backup object while editing I do need to copy these properties as well, so using this library can I override and hence also copy the properties tagged with json ignore? Thanks

StevenTCramer commented 3 years ago

I have a similar issue where I want to use JsonIgnore for one purpose but have AnyClone still clone the property.

replaysMike commented 3 years ago

this has been added as of 1.0.56 by making use of a CloneConfiguration when cloning an object. It allows you to specify a list of attribute names, or attribute types to ignore. By specifying an empty list you can override which attributes cause ignores, or none at all.

var test = new BasicObjectWithIgnore { 
    BoolValue = true,
    ByteValue = 1,
    IntValue = 100,
    LongValue = 1000,
    StringValue = "A test string" // contains JsonIgnoreAttribute
};
var clonedObject = test.Clone(new CloneConfiguration { IgnorePropertiesWithAttributes = new string[0] });
Assert.AreEqual(test, clonedObject);
Assert.IsNotNull(clonedObject.StringValue);