RSCG – TypeUtilities
| name | TypeUtilities |
| nuget | https://www.nuget.org/packages/TypeUtilities/ |
| link | https://github.com/DragonsLord/TypeUtilities |
| author | Yevhenii Serdiuk |
Pick/Omit for classes ( also have some mapping )
This is how you can use TypeUtilities .
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>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TypeUtilities" Version="0.0.1" />
</ItemGroup>
</Project>
The code that you will use is
using UtilDemo; var p=new PersonFull(); p.FirstName="Andrei"; p.LastName="Ignat"; //Person1 p1=(Person1)p ; //Person2 p2=(Person2)p ; Person1 p1 = new(); p1.FirstName = p.FirstName; Person2 p2=new(); p2.LastName = p.LastName; Console.WriteLine(p1.FirstName); Console.WriteLine(p2.LastName);
using TypeUtilities;
using static TypeUtilities.Abstractions.MemberDeclarationFormats;
using TypeUtilities.Abstractions;
namespace UtilDemo;
public class PersonFull
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int Salary { get; set; }
}
[Map(typeof(PersonFull),
MemberDeclarationFormat = $"{Tokens.Accessibility} string Mapped{Tokens.Name}{Tokens.Accessors}",
MemberKindSelection = MemberKindFlags.AnyProperty
)]
[Omit(typeof(PersonFull),nameof(PersonFull.Salary))]
public partial class Person2
{
}
[Pick(typeof(PersonFull),nameof(PersonFull.FirstName),nameof(PersonFull.LastName))]
public partial class Person1
{
}
The code that is generated is
namespace UtilDemo;
public partial class Person1
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
namespace UtilDemo;
public partial class Person2
{
public string MappedFirstName { get; set; }
public string MappedLastName { get; set; }
public string MappedSalary { get; set; }
}
namespace UtilDemo;
public partial class Person2
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/TypeUtilities
Leave a Reply