RhoMicro.CodeAnalysis.UtilityGenerators 20.0.0

dotnet add package RhoMicro.CodeAnalysis.UtilityGenerators --version 20.0.0
                    
NuGet\Install-Package RhoMicro.CodeAnalysis.UtilityGenerators -Version 20.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="20.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="20.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RhoMicro.CodeAnalysis.UtilityGenerators --version 20.0.0
                    
#r "nuget: RhoMicro.CodeAnalysis.UtilityGenerators, 20.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=20.0.0
                    
Install RhoMicro.CodeAnalysis.UtilityGenerators as a Cake Addin
#tool nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=20.0.0
                    
Install RhoMicro.CodeAnalysis.UtilityGenerators as a Cake Tool

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
  • Render blocks:
    • enclosed in (: and :)
    • for embedding (rendering) expressions or instantiating child templates.
  • Template blocks:
    • enclosed in <: and :>
    • to pass child content to child templates.

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.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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
20.0.0 446 3/26/2025
19.5.0 471 3/25/2025
19.4.0 134 3/17/2025
19.3.0 132 3/17/2025
19.2.0 134 3/17/2025
19.1.0 157 3/9/2025
19.0.2 156 3/9/2025
19.0.1 118 3/8/2025
19.0.0 144 3/8/2025
18.0.3 94 3/2/2025
18.0.2 87 3/2/2025
18.0.1 92 3/2/2025
17.1.3 86 1/24/2025
17.1.2 103 12/30/2024
17.1.1 90 12/30/2024
17.1.0 79 12/30/2024
17.0.2 91 12/24/2024
16.1.2 94 12/22/2024
16.1.1 93 12/21/2024
16.1.0 99 12/20/2024
16.0.4 112 12/18/2024
16.0.3 97 12/18/2024
16.0.0 112 12/18/2024
15.3.4 318 12/7/2024
15.3.3 97 12/7/2024
15.3.2 102 12/7/2024
15.3.1 106 12/7/2024
15.3.0 112 12/6/2024
15.2.4 117 12/6/2024
15.2.3 121 12/6/2024
15.1.7 241 7/15/2024
15.1.6 180 6/11/2024
15.1.5 192 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 179 3/4/2024
15.1.0 158 2/28/2024
15.0.1 134 2/22/2024
15.0.0 179 2/20/2024
14.0.5 160 2/19/2024
14.0.4 109 2/19/2024
14.0.3 118 2/19/2024
14.0.2 141 2/15/2024
14.0.0 138 2/15/2024
14.0.0-alpha.30 71 2/15/2024
14.0.0-alpha.29 62 2/15/2024
14.0.0-alpha.28 64 2/15/2024
14.0.0-alpha.27 67 2/15/2024
14.0.0-alpha.26 71 2/15/2024
14.0.0-alpha.25 69 2/15/2024
14.0.0-alpha.24 73 2/15/2024
14.0.0-alpha.22 69 2/15/2024
14.0.0-alpha.20 73 2/15/2024
13.0.0 233 1/8/2024
12.1.2 168 1/6/2024
12.1.1 151 1/6/2024
12.1.0 139 1/6/2024
12.0.9 183 1/5/2024
12.0.8 167 1/5/2024
11.0.0 167 12/16/2023
8.0.3 144 12/11/2023
8.0.2 162 12/11/2023
8.0.0 191 12/11/2023