Giter Site home page Giter Site logo

cloneable's Introduction

Cloneable

Auto generate Clone method using C# Source Generator

There are times you want to make a clone of an object. You can implement a clone method, but when a developer adds a new Field or Property the clone method should be changed too. Another way is to use reflection which is not performant. This source generator saves your time by generating the boilerplate code for cloning an object.

Installing Cloneable

You should install Cloneable with NuGet:

Install-Package Cloneable

Or via the .NET Core command line interface:

dotnet add package Cloneable

Either commands, from Package Manager Console or .NET Core CLI, will download and install Cloneable and all required dependencies.

Usage

You can add clone method to a class by making it partial and adding the attribute Cloneable on top of it. An example is provided in Cloneable.Sample project.

Source generators are introduced in dotnet 5.0. So make sure to have Visual Studio 16.8 or dotnet 5.0 sdk installed.

Here is a simple example:

[Cloneable]
public partial class Foo
{
    public string A { get; set; }
    public int B { get; set; }
}

For more examples please visit the sample project.

cloneable's People

Contributors

chargeproduction avatar mostmand avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

cloneable's Issues

Doesn't support inheritance

Would be nice if this could handle something like:

[Clonable]
class A {
    string Name { get; set; }
}

class B : A {
 ...
}

class C : A {
 ...
}

var c = new C();
c.Clone();

Support for Collection cloning

Hi!
Would like to use this package but without support for collections it's somewhat annoying to clone.

I have implemented most of the support for collections in my fork but wondering if you are still maintaining this?

Also converted most of the samples to tests. Let me know if this is something of intrest.

Included in reference list

Hi,

I've included your repository in my personal C# Source Generators list to help gather these kind of projects in one place and hopefully under single umbrella, thus I propose to add the csharp-sourcegenerator topic to this repository to help with visibility.

I hope you're okay with this. If you happen to know other generator projects, I'll be happy to enlist those in my list as well.

Thanks!

List Clone

Does this support list clone?

[Clonable]
public partial class ListItem {
   public int Key {get; set;}
}

[Clonable]
public partial class WithList {
   public List<ListItem> KeyItemList {get; set;} = new(){new(){Key=1}, new(){Key=2}}
}

I'm assuming this is not supported and the list is reassigned by reference?

Could this be supported, or is there a way I can add custom clone method for the KeyItemList property?

Support constructors?

I have this model

class Foo
{
    public Foo(int prop)
    {
        Prop = prop;
    }

    public int Prop {get;}
    public string Str {get;set;}
}

the generated method calls a default constructor which does not exist

P.S. If it is OK, I could try to prepare a PR for this issue. This issue does not sound impossible

Deep clone

Does this code do a deep clone, or does it only work for a shallow clone?

Does not support copying of lists

I found that if an object contains a list (like List), then nothing from it gets copied.
For the sample, I found if the parent has a count of 3 (because i added 3 integers), then the cloned has 0.
Which means that nothing got copied.
If the parent list was List, then what should happen is as follows:
it should loop through the list and add one by one so its a true deep copy.
however, still supporting the safeclone.

Generated code copies references

If you want to copy an object with nested objects the generated code would to something like this:

...
MyObjectProperty = this.MyObjectProperty

This would only copy the reference of the source object.

Missing license info

Currently, there is no license information available in this project.

Is this MIT licensed? Would it be possible to add a LICENSE.md to clarify? :)

support for records

please add support for records,

now this wont do anything

[Cloneable.Cloneable]
public partial record Foo(string Bar);

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.