CopyTo by Paul Braetz
Nuget / site data
Details
Info
Name: CopyTo
Generate a CopyTo
method for copying one instances property values to another.
Author: Paul Braetz
NuGet: https://www.nuget.org/packages/RhoMicro.CodeAnalysis.CopyToGenerator
You can find more details at https://github.com/PaulBraetz/RhoMicro.CodeAnalysis
Source : https://github.com/PaulBraetz/RhoMicro.CodeAnalysis
Original Readme
Rhomicro.CodeAnalysis
This repository contains my explorations on c# source code generation and analysis.
The [UnionsGenerator](https://github.com/PaulBraetz/RhoMicro.CodeAnalysis/UnionsGenerator generator enables the use of union types in C#.
The [UtilityGenerators](https://github.com/PaulBraetz/RhoMicro.CodeAnalysis/UtilityGenerators generator helps you write code generators and analyzers.
The [CopyTo](https://github.com/PaulBraetz/RhoMicro.CodeAnalysis/CopyTo generator generates methods to copy the contents of one instance to another. I created this generator for a friend, so it is not as feature rich as it could be.
About
Generating copy to code for properties of a class
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references CopyTo
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RhoMicro.CodeAnalysis.CopyToGenerator" Version="14.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of CopyTo in Program.cs
using CopyToDemo;
Person p = new();
p.FirstName = "Andrei";
p.LastName = "Ignat";
Person p2 = new();
p.CopyTo(p2);
Console.WriteLine(p2.FullName);
This is the use of CopyTo in Person.cs
namespace CopyToDemo;
[RhoMicro.CodeAnalysis.GenerateCopyTo]
internal partial class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- GenerateCopyToAttribute.g.cs
- Person.g.cs
namespace RhoMicro.CodeAnalysis;
using System;
[global::System.AttributeUsage(AttributeTargets.Class)]
#if GENERATOR
[RhoMicro.CodeAnalysis.GenerateFactory]
#endif
internal sealed partial class GenerateCopyToAttribute : global::System.Attribute { }
// <auto-generated>
// This file was last generated by the RhoMicro.CodeAnalysis.CopyToGenerator on 5/11/2024 7:36:26 PM +03:00
// </auto-generated>
#pragma warning disable
namespace CopyToDemo
{
partial class Person
{
/// <summary>
/// Copies this instances public properties to another ones.
/// </summary>
/// <param name = "target">The instance to copy this instances properties' values to.</param>
public void CopyTo(Person target)
{
if (this == target || target == null)
{
return;
}
if (AvoidCopy(this, target))
{
return;
}
target.FirstName = this.FirstName;
target.LastName = this.LastName;
}
/// <summary>
/// Evaluates whether copying between two instances of <see cref = "Person"/> should be avoided due to equivalence. This can help avoid unnecessary copying or infinite copying in nested recursive relationships.
/// </summary>
/// <param name = "a">The first instance to compare.</param>
/// <param name = "b">The second instance to compare.</param>
/// <param name = "result">Upon returning, contains <see langword="true"/> if copying between <paramref name = "a"/> and <paramref name = "b"/> should be avoided; otherwise, <see langword="false"/>.</param>
static partial void AvoidCopy(Person a, Person b, ref global::System.Boolean result);
/// <summary>
/// Evaluates whether copying between two instances of <see cref = "Person"/> should be avoided due to equivalence. This can help avoid unnecessary copying or infinite copying in nested recursive relationships.
/// </summary>
/// <param name = "a">The first instance to compare.</param>
/// <param name = "b">The second instance to compare.</param>
/// <returns><see langword="true"/> if copying between <paramref name = "a"/> and <paramref name = "b"/> should be avoided; otherwise, <see langword="false"/>.</returns>
static global::System.Boolean AvoidCopy(Person a, Person b)
{
{
var result = false;
AvoidCopy(a, b, ref result);
return result;
}
}
}
}
Usefull
Download Example (.NET C# )
Share CopyTo
https://ignatandrei.github.io/RSCG_Examples/v2/docs/CopyTo