RSCG – SourceGenerator.Helper.CopyCode

RSCG – SourceGenerator.Helper.CopyCode
 
 

name SourceGenerator.Helper.CopyCode
nuget https://www.nuget.org/packages/SourceGenerator.Helper.CopyCode/
link https://github.com/LokiMidgard/SourceGenerator.Helper.CopyCode
author Patrick Kranz

Transform Code to string

 

This is how you can use SourceGenerator.Helper.CopyCode .

The code that you start with is


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

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

  <ItemGroup>
    <PackageReference Include="SourceGenerator.Helper.CopyCode" Version="0.0.1">
      <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


Console.WriteLine(SourceGenerator.Helper.CopyCode.Copy.SourceGenerator_Helper_CopyCodeDemoNumberAttribute);
Console.WriteLine(SourceGenerator.Helper.CopyCode.Copy.SourceGenerator_Helper_CopyCodeDemoPerson);




namespace SourceGenerator_Helper_CopyCodeDemo;
[SourceGenerator.Helper.CopyCode.Copy]
[System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
internal sealed class NumberAttribute : Attribute
{
    public int ID;
}

[SourceGenerator.Helper.CopyCode.Copy]
internal class Person
{
    public int Id { get; set; }
}


 

The code that is generated is

// <auto-generated/>
#nullable enable

namespace SourceGenerator.Helper.CopyCode
{
	[global::System.CodeDom.Compiler.GeneratedCodeAttribute("SourceGenerator.Helper.CopyCode", "0.0.1.0")]
	[global::System.AttributeUsage(global::System.AttributeTargets.Enum | global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Interface, AllowMultiple = false)]
	internal sealed class CopyAttribute : global::System.Attribute
	{
	}
}

// <auto-generated/>
#nullable enable
namespace SourceGenerator.Helper.CopyCode;
internal  static partial class Copy {
    public const string SourceGenerator_Helper_CopyCodeDemoNumberAttribute = """
        // <auto-generated/>
        #nullable enable
        
        namespace SourceGenerator_Helper_CopyCodeDemo;
        [System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
        internal sealed class NumberAttribute : Attribute
        {
            public int ID;
        }
        """;
}

// <auto-generated/>
#nullable enable
namespace SourceGenerator.Helper.CopyCode;
internal  static partial class Copy {
    public const string SourceGenerator_Helper_CopyCodeDemoPerson = """
        // <auto-generated/>
        #nullable enable
        
        namespace SourceGenerator_Helper_CopyCodeDemo;
        internal class Person
        {
            public int Id { get; set; }
        }
        """;
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/SourceGenerator.Helper.CopyCode