GitXplorerGitXplorer
r

AnyClone

public
47 stars
7 forks
8 issues

Commits

List of commits on branch master.
Unverified
d40a1bd1ce26c212ead6836e5d497a4186bcb9e1

Revert Package license URL, it's now a deprecated feature

committed 2 years ago
Unverified
2c41272c7c0364373e51fa849e15244f8ed00162

Added MIT license URL for nuget

committed 2 years ago
Unverified
4a75453ee0b525d0146ca235f93e216fdb327ac7

Changed nuget package from GPL to MIT licensing

committed 2 years ago
Verified
1ae127834e4920fe5fb0c4d782a9074b3d7d8785

Changed from GPL to MIT licensing

rreplaysMike committed 2 years ago
Unverified
9f212cc87c4a667180aa58c6f2c0921dfa20716c

remove processing of static fields - this never should have been permitted in the first place

rreplaysMike committed 3 years ago
Unverified
bf835e8f5550a4e417c9808fec8855c6d94aaea1

fixes an error introduced during performance updates, constant fields are attempted to clone and should not be

rreplaysMike committed 3 years ago

README

The README file for this repository.

AnyClone

nuget nuget Build status Codacy Badge Codacy Badge

A CSharp library that can deep clone any object using only reflection.

No requirements for [Serializable] attributes, supports standard ignore attributes.

Description

I built this library as almost all others I tried on complex objects either didn't work at all, or failed to account for common scenarios. Serialization required too much boiler plate (BinarySerialization, Protobuf, or Json.Net) and fails to account for various designs. Implementing IClonable was too much of a chore and should be unnecessary. Various projects that use expression trees also failed to work, IObservable patterns were difficult to implement on large, already written code base.

Installation

Install AnyClone from the Package Manager Console:

PM> Install-Package AnyClone

Usage

using AnyClone.Extensions;

var originalObject = new SomeComplexTypeWithDeepStructure();
var myClonedObject = originalObject.Clone();

Capture Errors

using AnyClone.Extensions;

var originalObject = new SomeComplexTypeWithDeepStructure();
// capture errors found with your object where impossible situations occur, and add [IgnoreDataMember] to those properties/fields.

var myClonedObject = originalObject.Clone((ex, path, property, obj) => {
  Console.WriteLine($"Cloning error: {path} {ex.Message}");
  Assert.Fail();
});

Get differences between cloned objects using AnyDiff

using AnyClone.Extensions;
using AnyDiff;

var object1 = new MyComplexObject(1, "A string");
var object1Snapshot = object1.Clone();

var diff = AnyDiff.Diff(object1, object1Snapshot);
Assert.AreEqual(diff.Count, 0);

// change something anywhere in the object tree
object1.Name = "A different string";

diff = AnyDiff.Diff(object1, object1Snapshot);
Assert.AreEqual(diff.Count, 1);

Ignoring Properties/Fields

There are unfortunately a few situations that can't be resolved, such as cloning delegates, events etc. Fortunately, you can specify attributes that AnyClone will skip properties decorated with them. By default, AnyClone will ignore properties decorated with the following attributes: IgnoreDataMemberAttribute, NonSerializedAttribute, JsonIgnoreAttribute. If you wish to disable this behavior, or provide other attributes that you wish to decorate properties to ignore you can provide a custom CloneConfiguration:

using AnyClone;
using AnyClone.Extensions;

var originalObject = new SomeComplexTypeWithDeepStructure();
var myClonedObject = originalObject.Clone(CloneConfiguration.UsingAttributeNamesToIgnore("IgnoreDataMemberAttribute", "MyCustomAttribute"));

Both attribute names and attribute types can be specified:

var myClonedObject = originalObject.Clone(CloneConfiguration.UsingAttributeNamesToIgnore("IgnoreDataMemberAttribute", "MyCustomAttribute"));
var myOtherClonedObject = originalObject.Clone(CloneConfiguration.UsingAttributesToIgnore(typeof(IgnoreDataMemberAttribute), typeof(MyCustomAttribute)));

Other Applications

If you need to perform serialization instead of deep copying, try out AnySerializer which doesn't require attribute decoration and works on complex structures.