RSCG_UtilityTypes by Andrei Ignat
Nuget / site data
Details
Info
Name: RSCG_UtilityTypes
Roslyn Utility Types - Pick, Omit
Author: Andrei Ignat
NuGet: https://www.nuget.org/packages/RSCG_UtilityTypes/
https://www.nuget.org/packages/RSCG_UtilityTypesCommon
You can find more details at https://github.com/ignatandrei/RSCG_UtilityTypes
Original Readme
RSCG_UtilityTypes
Omit and Pick from TypeScript : https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys
generating also extension for converting from one type to another . See demo project.
Usage
Add to your csproj file
<ItemGroup>
<PackageReference Include="RSCG_UtilityTypes" Version="2023.1223.1230" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="RSCG_UtilityTypesCommon" Version="2023.1223.1230" />
</ItemGroup>
In C# 9.0
[Omit("MoviePreviewSmall", nameof(Actors),nameof(Year))]
[Pick("MoviePreviewMinimal", nameof(Title), nameof(Year))]
public class Movie
{
public string? Title { get; set; }
public string? Director { get; set; }
public int Year { get; set; }
public string[]? Actors { get; set; }
}
And 2 new classes will be generated , MoviePreviewSmall and MoviePreviewMinimal
About
Add omit and pick to selectively generate types from existing types
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references RSCG_UtilityTypes
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RSCG_UtilityTypes" Version="2023.1223.1230" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="RSCG_UtilityTypesCommon" Version="2023.1223.1230" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of RSCG_UtilityTypes in Program.cs
using UtilDemo;
var p=new PersonFull();
p.FirstName="Andrei";
p.LastName="Ignat";
Person1 p1=(Person1)p ;
Person2 p2=(Person2)p ;
Console.WriteLine(p1.FirstName);
Console.WriteLine(p2.LastName);
This is the use of RSCG_UtilityTypes in Person.cs
using RSCG_UtilityTypesCommon;
namespace UtilDemo;
[Pick("Person1",nameof(FirstName),nameof(LastName))]
[Omit("Person2", nameof(Salary))]
public class PersonFull
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Salary { get; set; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- PersonFull_Person1.cs
- PersonFull_Person2.cs
namespace UtilDemo
{
partial class Person1
{
public string FirstName { get; set; }
public string LastName { get; set; }
public static explicit operator Person1(PersonFull data )
{
var ret= new Person1 ();
ret.FirstName = data.FirstName;
ret.LastName = data.LastName;
return ret;
}
public static explicit operator PersonFull(Person1 data )
{
var ret= new PersonFull ();
ret.FirstName = data.FirstName;
ret.LastName = data.LastName;
return ret;
}
}
}
namespace UtilDemo
{
partial class Person2
{
public string FirstName { get; set; }
public string LastName { get; set; }
public static explicit operator Person2(PersonFull data )
{
var ret= new Person2 ();
ret.FirstName = data.FirstName;
ret.LastName = data.LastName;
return ret;
}
public static explicit operator PersonFull(Person2 data )
{
var ret= new PersonFull ();
ret.FirstName = data.FirstName;
ret.LastName = data.LastName;
return ret;
}
}
}
Usefull
Download Example (.NET C# )
Share RSCG_UtilityTypes
https://ignatandrei.github.io/RSCG_Examples/v2/docs/RSCG_UtilityTypes