SourceGenerator.Helper.CopyCode by Patrick Kranz
Nuget / site data
Details
Info
Name: SourceGenerator.Helper.CopyCode
This Generator is intendede to generate text that a source generator can use to emit source to its generation. (See ReadMe)
Author: Patrick Kranz
NuGet: https://www.nuget.org/packages/SourceGenerator.Helper.CopyCode/
You can find more details at https://github.com/LokiMidgard/SourceGenerator.Helper.CopyCode
Source : https://github.com/LokiMidgard/SourceGenerator.Helper.CopyCode
Original Readme
SourceGenerator.Helper.CopyCode
This Generator is intendede to generate text that a source generator can use to emit source to its generation.
E.g. Instead of writing a String that contains the definiton of an Attribute (without syntax highlighting and checking).
You can generate the attribute normaly in Code and anotate it wit [SourceGenerator.Helper.CopyCode.Copy]
.
Attributes defined on that Type will also be copied, if they are defined below the [SourceGenerator.Helper.CopyCode.Copy]
-Attribute.
Assume you have the following attribute:
namespace SourceGenerator.Helper.CopyCode.Example;
[SourceGenerator.Helper.CopyCode.Copy]
[System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
internal sealed class MyGeneratorAttribute : Attribute {
}
then the generator will generate:
// <auto-generated/>
#nullable enable
namespace SourceGenerator.Helper.CopyCode;
internal static partial class Copy {
public const string SourceGeneratorHelperCopyCodeExampleMyGeneratorAttribute = """
// <auto-generated/>
#nullable enable
namespace SourceGenerator.Helper.CopyCode.Example;
[System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
internal sealed class MyGeneratorAttribute : Attribute
{
}
""";
}
And your Generator can emit it:
[Generator(LanguageNames.CSharp)]
public class MyGenerator : IIncrementalGenerator {
public void Initialize(IncrementalGeneratorInitializationContext context) {
context.RegisterPostInitializationOutput(context => context.AddSource("attribute.g.cs", SourceGenerator.Helper.CopyCode.Copy.SourceGeneratorHelperCopyCodeExampleMyGeneratorAttribute ));
// The rest of your generator�
}
}
About
Transform Code to string
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references SourceGenerator.Helper.CopyCode
<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>
This is the use of SourceGenerator.Helper.CopyCode in Program.cs
Console.WriteLine(SourceGenerator.Helper.CopyCode.Copy.SourceGenerator_Helper_CopyCodeDemoNumberAttribute);
Console.WriteLine(SourceGenerator.Helper.CopyCode.Copy.SourceGenerator_Helper_CopyCodeDemoPerson);
This is the use of SourceGenerator.Helper.CopyCode in Person.cs
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; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- CopyAttribute.g.cs
- SourceGenerator_Helper_CopyCodeDemo.NumberAttribute.Copy.g.cs
- SourceGenerator_Helper_CopyCodeDemo.Person.Copy.g.cs
// <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; }
}
""";
}
Usefull
Download Example (.NET C# )
Share SourceGenerator.Helper.CopyCode
https://ignatandrei.github.io/RSCG_Examples/v2/docs/SourceGenerator.Helper.CopyCode