RSCG – CopyTo
| name | CopyTo |
| nuget | https://www.nuget.org/packages/RhoMicro.CodeAnalysis.CopyToGenerator |
| link | https://github.com/PaulBraetz/RhoMicro.CodeAnalysis |
| author | Paul Braetz |
Generating copy to code for properties of a class
This is how you can use CopyTo .
The code that you start with is
<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>
The code that you will use is
using CopyToDemo; Person p = new(); p.FirstName = "Andrei"; p.LastName = "Ignat"; Person p2 = new(); p.CopyTo(p2); Console.WriteLine(p2.FullName);
namespace CopyToDemo;
[RhoMicro.CodeAnalysis.GenerateCopyTo]
internal partial class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
The code that is generated is
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 2/18/2024 10:52:42 AM +02: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;
}
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/CopyTo
Leave a Reply