TypeUtilities by Yevhenii Serdiuk
Nuget / site data
Details
Info
Name: TypeUtilities
A set of type utilities to transform types. Include utils like:
- Pick
- Omit
Author: Yevhenii Serdiuk
NuGet: https://www.nuget.org/packages/TypeUtilities/
You can find more details at https://github.com/DragonsLord/TypeUtilities
Original Readme
TypeUtilities
Type Utilities provides a source generators to create/transform one type into another.
This project was inspired by the TypeScript Utility Types and was ment to bring similar functionality to the C# via source generators
Installation
To use the the TypeUtilities, install the TypeUtilities package into your project.
To install the packages, add the references to your csproj file, for example by running
dotnet add package TypeUtilities
This adds a <PackageReference>
to your project.
Usage
TypeUtilities provides several attributes:
Map
Map Attribute simply maps memebers of the source type to the target type using specified format.
using TypeUtilities;
using TypeUtilities.Abstractions;
public class SourceType
{
public Guid Id { get; }
public int Value { get; set; }
public DateTime Created { get; set; }
}
[Map(typeof(SourceType))]
public partial class SimpleMap
{
}
// Generated result
//----- SimpleMap.map.SourceType.g.cs
public partial class SimpleMap
{
public System.Guid Id { get; }
public int Value { get; set; }
public System.DateTime Created { get; set; }
}
// --------------------
[Map(typeof(SourceType),
MemberDeclarationFormat = $"{Tokens.Accessibility} string Mapped{Tokens.Name}{Tokens.Accessors}",
MemberKindSelection = MemberKindFlags.ReadonlyProperty
)]
public partial class AdvancedMap
{
}
// Generated result
//----- AdvancedMap.map.SourceType.g.cs
public partial class AdvancedMap
{
public string MappedId { get; }
}
// --------------------
More detailed description for Map is provided here
Omit
Omit Attribute is similar to Map but also accepts an explicit list of members that should be exluded
using TypeUtilities;
public class SourceType
{
public Guid Id { get; }
public int Value { get; set; }
public DateTime Created { get; set; }
}
[Omit(typeof(SourceType), "Value")]
public partial class TargetType
{
public int MyValue { get; set; }
}
// Generated result
//----- TargetType.omit.SourceType.g.cs
public partial class TargetType
{
public Guid Id { get; }
public DateTime Created { get; set; }
}
Pick
Pick Attribute is similar to Map but also requires to explicitly specify all members that should be included
using TypeUtilities;
public class SourceType
{
public Guid Id { get; }
public int Value { get; set; }
public DateTime Created { get; set; }
}
[Pick(typeof(SourceType), "Id", nameof(SourceType.Value))]
public partial class TargetType
{
}
// Generated result
//----- TargetType.omit.SourceType.g.cs
public partial class TargetType
{
public Guid Id { get; }
public int Value { get; set; }
}
About
Pick/Omit for classes ( also have some mapping )
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references TypeUtilities
<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>
This is the use of TypeUtilities in Program.cs
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);
This is the use of TypeUtilities in Person.cs
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
{
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Person1.pick.PersonFull.g.cs
- Person2.map.PersonFull.g.cs
- Person2.omit.PersonFull.g.cs
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; }
}
Usefull
Download Example (.NET C# )
Share TypeUtilities
https://ignatandrei.github.io/RSCG_Examples/v2/docs/TypeUtilities