RSCG – Tortuga.Shipwright

 
 

name Tortuga.Shipwright
nuget https://www.nuget.org/packages/Tortuga.Shipwright/
link https://github.com/TortugaResearch/Tortuga.Shipwright
author Tortuga Research

Generate mixin between classes

 

This is how you can use Tortuga.Shipwright .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>


	<!-- Code Generator -->
	<ItemGroup>
		<PackageReference Include="Tortuga.Shipwright" Version="0.9.0" >
			<PrivateAssets>all</PrivateAssets>
		</PackageReference>
		<PackageReference Include="Tortuga.Shipwright.Shared" Version="0.9.0" />
	</ItemGroup>

	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

	<ItemGroup>
		<!-- Don't include the output from a previous source generator execution into future runs; the */** trick here ensures that there's
at least one subdirectory, which is our key that it's coming from a source generator as opposed to something that is coming from
some other tool. -->
		<Compile Remove="$(CompilerGeneratedFilesOutputPath)/*/**/*.cs" />
	</ItemGroup>
</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using MixinConsoleDemo;

Console.WriteLine("Hello, World!");
Employee p = new Employee();
p.Name="Andrei Ignat";
p.Age = 55;
p.Salary = 1000;
Console.WriteLine($"Name: {p.Name}, Age: {p.Age}, Salary: {p.Salary}");


using Tortuga.Shipwright;

namespace MixinConsoleDemo;
internal class Person
{
    [Expose]
    public string Name { get; set; } = string.Empty;
    [Expose] 
    public int Age { get; set; }
    
}



using Tortuga.Shipwright;

namespace MixinConsoleDemo;
[UseTrait(typeof(Person))]
internal partial class Employee
{
    public decimal Salary { get; set; }
}


 

The code that is generated is

/*
Container class: MixinConsoleDemo.Employee
	Adding trait: MixinConsoleDemo.Person
*/
//This file was generated by Tortuga Shipwright

namespace MixinConsoleDemo
{
	partial class Employee
	{

		// These fields and/or properties hold the traits. They should not be referenced directly.
		private MixinConsoleDemo.Person __Trait0 = new();

		// Exposing trait MixinConsoleDemo.Person

		
		public   System.Int32 Age
		{
			get => __Trait0.Age;
			set => __Trait0.Age = value;
		}
		
		public   System.String Name
		{
			get => __Trait0.Name;
			set => __Trait0.Name = value;
		}
	}
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Tortuga.Shipwright


Posted

in

, ,

by

Tags: