RhoMicro.CodeAnalysis.UtilityGenerators
19.1.0
dotnet add package RhoMicro.CodeAnalysis.UtilityGenerators --version 19.1.0
NuGet\Install-Package RhoMicro.CodeAnalysis.UtilityGenerators -Version 19.1.0
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="19.1.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add RhoMicro.CodeAnalysis.UtilityGenerators --version 19.1.0
#r "nuget: RhoMicro.CodeAnalysis.UtilityGenerators, 19.1.0"
// Install RhoMicro.CodeAnalysis.UtilityGenerators as a Cake Addin #addin nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=19.1.0 // Install RhoMicro.CodeAnalysis.UtilityGenerators as a Cake Tool #tool nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=19.1.0
What is this?
This project contains generators and analyzers that help writing generators and analyzers:
Installation
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Attribute Factory
TODO
File Inclusion
Annotate with the IncludeFileAttribute
any type or assembly in order to generate code
that includes the file containing the target in a generator context via
RegisterPostInitializationOutput
.
Example:
namespace MyNamespace;
using System;
using RhoMicro.CodeAnalysis;
[IncludeFile]
internal class Foo
{
public static Int32 Bar() => 42;
}
Foo.cs
// <auto-generated>
// This file was generated by the RhoMicro.CodeAnalysis.FileInclusionGenerator.
// </auto-generated>
using Microsoft.CodeAnalysis;
using System;
namespace RhoMicro.CodeAnalysis.Generated
{
internal static class IncludedFileSources
{
public static void RegisterToContext(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(c =>
{c.AddSource(
"RhoMicro_CodeAnalysis_Foo.g.cs",
$$"""
// <auto-generated>
// This file was generated by RhoMicro.CodeAnalysis.FileInclusionGenerator.
// </auto-generated>
#pragma warning disable
namespace RhoMicro.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[IncludeFile]
internal class Foo
{
public static Int32 Bar() => 42;
}
""");
}); }
}
}
IncludedFiles.g.cs
NonEquatable
Annotate types with the NonEquatableAttribute
to generate throwing implementations for Equals
and GetHashCode
. This generator helps detect illegal objects in incremental generator pipelines.
Example:
namespace MyNamespace;
using System;
using RhoMicro.CodeAnalysis;
[NonEquatable]
internal partial class Foo
{
public static Int32 Bar() => 42;
}
Foo.cs
// <auto-generated>
// This file was generated by RhoMicro.CodeAnalysis.NonEquatableGenerator
// The tool used to generate this code may be subject to license terms;
// this generated code is however not subject to those terms, instead it is
// subject to the license (if any) applied to the containing project.
// </auto-generated>
#pragma warning disable
#nullable enable
namespace MyNamespace;
partial class Foo
{
/// <inheritdoc/>
public sealed override bool Equals(object obj) => throw new global::System.NotSupportedException("global::MyNamespace.Foo.Equals(object) is not supported.");
/// <inheritdoc/>
public bool Equals(global::MyNamespace.Foo obj) => throw new global::System.NotSupportedException("global::MyNamespace.Foo.Equals(global::MyNamespace.Foo) is not supported.");
/// <inheritdoc/>
public sealed override int GetHashCode() => throw new global::System.NotSupportedException("global::MyNamespace.Foo.GetHashCode() is not supported.");
}
MyNamespace_Foo.g.cs
Type Symbol Pattern
Annotate static partial methods with the TypeSymbolPattern
attribute to generate an implementation
matching a pattern of the attributes type parameter against the method symbol paremeter.
Example:
namespace MyNamespace;
using System;
using Microsoft.CodeAnalysis;
using RhoMicro.CodeAnalysis;
internal static partial class Foo
{
[TypeSymbolPattern(typeof(List<String>))]
public static partial Boolean IsStream(ITypeSymbol symbol);
}
Foo.cs
// <auto-generated>
// This file was generated by RhoMicro.CodeAnalysis.TypeSymbolPatternGenerator
// The tool used to generate this code may be subject to license terms;
// this generated code is however not subject to those terms, instead it is
// subject to the license (if any) applied to the containing project.
// </auto-generated>
#pragma warning disable
#nullable enable
namespace MyNamespace;
partial class Foo
{
public static partial bool IsStringList(global::Microsoft.CodeAnalysis.ITypeSymbol? type)
{
var result = type is global::Microsoft.CodeAnalysis.INamedTypeSymbol
{
Name: "List",
TypeArguments:[global::Microsoft.CodeAnalysis.INamedTypeSymbol
{
Name: "String",
TypeArguments:[],
ContainingNamespace:
{
Name: "System",
ContainingNamespace:
{
IsGlobalNamespace: true
}
}
}
],
ContainingNamespace:
{
Name: "Generic",
ContainingNamespace:
{
Name: "Collections",
ContainingNamespace:
{
Name: "System",
ContainingNamespace:
{
IsGlobalNamespace: true
}
}
}
}
}
;
return result;
}
public static bool IsStringList(global::Microsoft.CodeAnalysis.ITypeSymbol? type, [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out global::Microsoft.CodeAnalysis.INamedTypeSymbol? outType)
{
var result = IsStringList(type);
outType = result ? (global::Microsoft.CodeAnalysis.INamedTypeSymbol) type : null;
return result;
}
}
MyNamespace_Foo.g.cs_
Note that containing types are not currently supported.
Templating
A string templating engine is provided via the Template
attribute.
The templating syntax uses the following delimiters:
- Code blocks:
- enclosed in
{:
and:}
- for C# code
- enclosed in
- Render blocks:
- enclosed in
(:
and:)
- for embedding (rendering) expressions or instantiating child templates.
- enclosed in
- Template blocks:
- enclosed in
<:
and:>
- to pass child content to child templates.
- enclosed in
Examples
Simple Text-Only template
namespace MyNamespace;
using RhoMicro.CodeAnalysis;
[Template(
"""
Hello, world!
Welcome to our templating engine demo.
""")]
internal partial class Foo;
Foo.cs
// <auto-generated>
// This file was generated by RhoMicro.CodeAnalysis.TemplatingGenerator
// The tool used to generate this code may be subject to license terms;
// this generated code is however not subject to those terms, instead it is
// subject to the license (if any) applied to the containing project.
// </auto-generated>
#pragma warning disable
#nullable enable
using static global::RhoMicro.CodeAnalysis.Library.Text.Templating.Indentations;
namespace MyNamespace;
partial class Foo :
global::RhoMicro.CodeAnalysis.Library.Text.Templating.ITemplate
{
public override string ToString() => global::RhoMicro.CodeAnalysis.Library.Text.Templating.TemplateRenderer.Render(this);
public string ToString(global::System.Threading.CancellationToken cancellationToken) => global::RhoMicro.CodeAnalysis.Library.Text.Templating.TemplateRenderer.Render(this, cancellationToken);
public void Render<__TBody>(ref global::RhoMicro.CodeAnalysis.Library.Text.Templating.TemplateRenderer __renderer, __TBody __body)
where __TBody : global::RhoMicro.CodeAnalysis.Library.Text.Templating.ITemplate
{
__renderer.ThrowIfCancellationRequested();
// This template was taken from: F:\dev\git\RhoMicro.CodeAnalysis\UtilityGenerators.Tests.E2E\Templating\Foo.cs(6,0)
const string __template =
"""
Hello, world!
Welcome to our templating engine demo.
""";
__renderer.Render(__template.AsSpan(0, 52));
}
}
MyNamespace_Foo.g.cs
Building The Project
This project is running the generators contained against itself. To do this, run the bootstrap.ps1
script.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.12.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
19.1.0 | 78 | 3/9/2025 |
19.0.2 | 78 | 3/9/2025 |
19.0.1 | 78 | 3/8/2025 |
19.0.0 | 104 | 3/8/2025 |
18.0.3 | 90 | 3/2/2025 |
18.0.2 | 83 | 3/2/2025 |
18.0.1 | 88 | 3/2/2025 |
17.1.3 | 82 | 1/24/2025 |
17.1.2 | 100 | 12/30/2024 |
17.1.1 | 88 | 12/30/2024 |
17.1.0 | 77 | 12/30/2024 |
17.0.2 | 89 | 12/24/2024 |
16.1.2 | 92 | 12/22/2024 |
16.1.1 | 90 | 12/21/2024 |
16.1.0 | 97 | 12/20/2024 |
16.0.4 | 110 | 12/18/2024 |
16.0.3 | 95 | 12/18/2024 |
16.0.0 | 104 | 12/18/2024 |
15.3.4 | 274 | 12/7/2024 |
15.3.3 | 95 | 12/7/2024 |
15.3.2 | 100 | 12/7/2024 |
15.3.1 | 103 | 12/7/2024 |
15.3.0 | 110 | 12/6/2024 |
15.2.4 | 114 | 12/6/2024 |
15.2.3 | 118 | 12/6/2024 |
15.1.7 | 238 | 7/15/2024 |
15.1.6 | 180 | 6/11/2024 |
15.1.5 | 191 | 3/26/2024 |
15.1.4 | 130 | 3/26/2024 |
15.1.3 | 217 | 3/11/2024 |
15.1.2 | 151 | 3/8/2024 |
15.1.1 | 178 | 3/4/2024 |
15.1.0 | 157 | 2/28/2024 |
15.0.1 | 130 | 2/22/2024 |
15.0.0 | 177 | 2/20/2024 |
14.0.5 | 159 | 2/19/2024 |
14.0.4 | 107 | 2/19/2024 |
14.0.3 | 116 | 2/19/2024 |
14.0.2 | 140 | 2/15/2024 |
14.0.0 | 137 | 2/15/2024 |
14.0.0-alpha.30 | 70 | 2/15/2024 |
14.0.0-alpha.29 | 61 | 2/15/2024 |
14.0.0-alpha.28 | 63 | 2/15/2024 |
14.0.0-alpha.27 | 66 | 2/15/2024 |
14.0.0-alpha.26 | 70 | 2/15/2024 |
14.0.0-alpha.25 | 68 | 2/15/2024 |
14.0.0-alpha.24 | 72 | 2/15/2024 |
14.0.0-alpha.22 | 68 | 2/15/2024 |
14.0.0-alpha.20 | 72 | 2/15/2024 |
13.0.0 | 232 | 1/8/2024 |
12.1.2 | 167 | 1/6/2024 |
12.1.1 | 150 | 1/6/2024 |
12.1.0 | 138 | 1/6/2024 |
12.0.9 | 182 | 1/5/2024 |
12.0.8 | 166 | 1/5/2024 |
11.0.0 | 166 | 12/16/2023 |
8.0.3 | 143 | 12/11/2023 |
8.0.2 | 161 | 12/11/2023 |
8.0.0 | 190 | 12/11/2023 |