Coder Social home page Coder Social logo

georgecloney's Introduction

GeorgeCloney

Useful extension methods for deep-cloning an object.

There are 3 extension methods available:

  • Serialize(this object source) returns uses the BinaryFormatter to return a MemoryStream containing the serialized object.
  • Deserialize(this Stream stream) uses BinaryFormatter to recreate an instance of T from the stream.
  • DeepClone(this object source) will return a copy of the object with the same values but not the same instance. The clone is created either using serialization, if the source is serializable, or reflection if it is not. When using reflection all properties are recursed so that it really is a "deep" clone.
	class Test
	{
		public string Value { get; set; }
		public Test Nested { get; set; }
	}
	
	var A = new Test { Value = "something", Nested = new Test { Value = "else" } };
	var B = A.DeepClone();
	
	// A and B are equal but not the same
	Assert.AreEqual(A.Value, B.Value);
	Assert.AreEqual(A.Nested.Value, B.Nested.Value);
	
	// Modifying B does not change A
	B.Nested.Value = "modified";
	Assert.AreNotEqual(A.Nested.Value, B.Nested.Value);

Get GeorgeCloney at NuGet.org: https://www.nuget.org/packages/GeorgeCloney/

georgecloney's People

Contributors

laazyj avatar mcdoit avatar steenlknudsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

georgecloney's Issues

Doesn't work with netstandard

Hi,

I've been using this library for a while and I like ti very much. It is also one of the most popular cloning libraries on nuget.

However I've recently started porting my project to netstandard and the compiler is complaining, that GeorgeCloney needs the old dotnet 4.x. Therefore I'm stuck.

Since there aren't any external dependencies in GeorgeCloney, I think it wouldn't be too hard to target this library to netstandard instead of net 4.x (e.g. usually netstandard 1.3 is the lowest common denomintor that works anywhere).

Any chance you'd be willing to do that.

How can I *exclude* one or more properties from being cloned?

Assume I have a class like this:

public class Person
{
    public int PersonId { get; set; }

    public string Name { get; set; }
    public string Firstname { get; set; }
}

It is not marked as serializable - just a plain old CLR class.

How can I tell GeorgeCloney not to clone my "primary key" - "PersonId" ? I tried [NonSerialized] but that can only be applied to fields - not properties. Any other way?

Does not clone fields from base classes.

DeepCloneWithoutSerialization does not clone fields from base classes.

This unit test fails:

public class A
{
    public string Name { get; set; }
}

public class B : A
{
    public string Message { get; set; }
}

[TestFixture]
[Category("Unittest")]
public class ClonerTest
{
    [Test]
    public void DebetKreditTest1()
    {
        var a = new A();
        a.Name = "test1";

        var clone1 = a.DeepCloneWithoutSerialization();

        Assert.IsTrue(a.Name == clone1.Name);

        var b = new B();
        b.Name = "test2";
        b.Message = "test2a";

        var clone2 = b.DeepCloneWithoutSerialization();

        Assert.IsTrue(b.Message == clone2.Message);
        Assert.IsTrue(b.Name == clone2.Name);
    }

Doesn't properly work with "object" properties

If your class has a property that is declared as an 'System.Object', but a instance of a proper class is there, GeorgeCloney creates a new plain object. The desired effect would be to have the actual instance of the type cloned.

class Foo
{
    public object SomeObject { get; set; }
}

class Bar
{
    public int SomeValue { get; set; }
}

var foo = new Foo { SomeObject = new Bar { SomeValue = 10 } };
var clone = foo.DeepClone();
Console.WriteLine(clone.Bar is Bar); // prints False 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.