RSCG – UtilityVerse.Copy

RSCG – UtilityVerse.Copy
 
 

name UtilityVerse.Copy
nuget https://www.nuget.org/packages/UtilityVerse.Copy/
link https://github.com/purkayasta/TheUtilityVerse
author pritom purkayasta

Deep Clone and Shallow Copy of objects

 

This is how you can use UtilityVerse.Copy .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<ItemGroup>
	  <PackageReference Include="UtilityVerse.Copy" Version="0.5.0" />
	</ItemGroup>
</Project>


The code that you will use is


using CloneData;

Console.WriteLine("Hello, World!");
Person p = new ();
p.FirstName = "Andrei";
p.LastName = "Ignat";
p.Age = 54;
var p1=p.DeepCopy();
Console.WriteLine(p1.Name());



using UtilityVerse.Copy;

namespace CloneData;
[DeepCopy]
public partial class Person
{
    public string FirstName { get; set; } = "";
    public string LastName { get; set; } = "";
    public int Age { get; set; }
    public string Name() => $"{FirstName} {LastName}";

    public Person[] Childs { get; set; } = [];
}


 

The code that is generated is

using System;

namespace Dolly
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
    public class ClonableAttribute : Attribute
    {
    }
}
using System;

namespace Dolly
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public class CloneIgnoreAttribute : Attribute
    {
    }
}
using System;
namespace Dolly
{
    public interface IClonable<T> : ICloneable
    {
        T DeepClone();
        T ShallowClone();
    }
}
using Dolly;
using System.Linq;
namespace CloneData;
partial class Person : IClonable<Person>
{
    object ICloneable.Clone() => this.DeepClone();
    public virtual Person DeepClone() =>
        new ()
        {
            FirstName = FirstName,
            LastName = LastName,
            Childs = Childs.Select(item => item.DeepClone()).ToArray()
        };

    public virtual Person ShallowClone() =>
        new ()
        {
            FirstName = FirstName,
            LastName = LastName,
            Childs = Childs.ToArray()
        };
}

  // <auto-generated>
  //     This code was generated by Copy.
  //     Author: Pritom Purkayasta
  //     DO NOT modify this file manually. Changes may be overwritten.
  //     This file contains auto-generated DeepCopy() implementations.
  // </auto-generated>

  using System;
  using System.Linq;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Collections.Immutable;
  using System.Collections.Concurrent;
  using System.Collections.Frozen;

namespace CloneData
{
    public partial class Person
    {
        [System.CodeDom.Compiler.GeneratedCode("DeepCopyGenerator", "1.0")]
        public Person DeepCopy()
        {
            return new Person
            {
                FirstName = this.FirstName,
                LastName = this.LastName,
                Age = this.Age,
                Childs = this.Childs?.Select(x => x?.DeepCopy()).ToArray(),
            };
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/UtilityVerse.Copy


Posted

in

, ,

by

Tags: