RhoMicro.CodeAnalysis.UtilityGenerators 14.0.0-alpha.24

Prefix Reserved
This is a prerelease version of RhoMicro.CodeAnalysis.UtilityGenerators.
There is a newer version of this package available.
See the version list below for details.
dotnet add package RhoMicro.CodeAnalysis.UtilityGenerators --version 14.0.0-alpha.24
                    
NuGet\Install-Package RhoMicro.CodeAnalysis.UtilityGenerators -Version 14.0.0-alpha.24
                    
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="14.0.0-alpha.24">
  <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="14.0.0-alpha.24" />
                    
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 14.0.0-alpha.24
                    
#r "nuget: RhoMicro.CodeAnalysis.UtilityGenerators, 14.0.0-alpha.24"
                    
#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.
#:package RhoMicro.CodeAnalysis.UtilityGenerators@14.0.0-alpha.24
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=14.0.0-alpha.24&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=RhoMicro.CodeAnalysis.UtilityGenerators&version=14.0.0-alpha.24&prerelease
                    
Install as a Cake Tool

What is this?

This project contains generators and analyzers that help writing generators and analyzers.

Currently, there are two generators contained within:

Installation

<PackageReference Include="RhoMicro.CodeAnalysis.UtilityGenerators" Version="*">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

LibraryGenerator

Generates types that help with source code generators & analyzers. A more detailed section is coming soon.

AttributeFactoryGenerator

Are you creating source generators for C#? Are you using attributes to allow your consumers to instruct your generator? Are you dissatisfied with the amount of boilerplate you have to write in order to extract those instructions from the roslyn api?

Then this project could be of use to you!

Key Features & Limitations

  • Generate Factory for parsing attribute instance from AttributeData
  • Generate helper functions for retrieving instances of your attribute from an IEnumerable<AttributeData>
  • Parse type properties as ITypeSymbols
  • Generate the attribute source text itself (no requirement for second attribute library)

How to use

Add a compilation flag to your generator project:

<PropertyGroup>
    <DefineConstants>$(DefineConstants);GENERATOR</DefineConstants>
</PropertyGroup>

Declare an attribute like so:

[AttributeUsage(AttributeTargets.Class)]
#if GENERATOR
[RhoMicro.CodeAnalysis.GenerateFactory]
#endif
public partial class TestGeneratorTargetAttribute : Attribute
{
#if GENERATOR
    [ExcludeFromFactory]
    private TestGeneratorTargetAttribute(System.Object typeSymbolContainer) =>
        _typeSymbolContainer = typeSymbolContainer;
#endif
    public TestGeneratorTargetAttribute(String name, Int32[] ages)
    {
        Name = name;
        Ages = ages;
    }
    public TestGeneratorTargetAttribute(Type type) => Type = type;

    public String Name { get; }
    public Int32[] Ages { get; }
    public Type? Type { get; set; }
}

The constructor enclosed in the preprocessor condition is required in order to construct an instance when the consumer made use of a constructor taking at least one parameter of type Type.

For every constructor that takes at least one parameter of type Type, an equivalent factory constructor is required. These are expected to take an instance of Object instead of the type and assign it to the generated helper field.

This way, a generated helper property of type ITypeSymbol may be used to retrieve the type used by the consumer in their typeof expression.

Use the generated factory and helper methods like so:

ImmutableArray<AttributeData> attributes = 
    symbol.GetAttributes();

IEnumerable<TestGeneratorTargetAttribute> allParsed =
    attributes.OfTestGeneratorTargetAttribute();

TestGeneratorTargetAttribute singleParsed =
    TestGeneratorTargetAttribute.TryCreate(attributes[0], out var a) ? 
    a : 
    null;

singleParsed = 
    symbol.TryGetFirstTestGeneratorTargetAttribute(out var a) ? 
    a : 
    null;

context.RegisterPostInitializationOutput(
    c => c.AddSource($"{nameof(TestGeneratorTargetAttribute)}.g.cs", TestGeneratorTargetAttribute.SourceText));

The generated extension method OfTestGeneratorTargetAttribute will return all instances of TestGeneratorTargetAttribute found in the symbols list of attributes.

The generated extension method TryGetFirstTestGeneratorTargetAttribute attempts to retrieve the first instance of TestGeneratorTargetAttribute found on the symbol.

The generated extension method ForTestGeneratorTargetAttribute makes use of the efficient FAWMN api.

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
23.1.0 31 12/21/2025
23.0.0 38 12/21/2025
22.0.3 259 8/20/2025
22.0.2 173 8/20/2025
22.0.1 180 7/12/2025
21.1.2 197 6/25/2025
21.1.0 194 6/25/2025
21.0.6 184 6/24/2025
20.1.5 421 5/22/2025
20.1.4 158 5/17/2025
20.1.2 178 5/11/2025
20.0.0 625 3/26/2025
19.5.0 531 3/25/2025
19.4.0 191 3/17/2025
19.3.0 188 3/17/2025
19.2.0 183 3/17/2025
19.1.0 217 3/9/2025
19.0.2 211 3/9/2025
19.0.1 171 3/8/2025
19.0.0 208 3/8/2025
18.0.3 139 3/2/2025
18.0.2 143 3/2/2025
18.0.1 146 3/2/2025
17.1.3 142 1/24/2025
17.1.2 153 12/30/2024
17.1.1 142 12/30/2024
17.1.0 137 12/30/2024
17.0.2 134 12/24/2024
16.1.2 149 12/22/2024
16.1.1 147 12/21/2024
16.1.0 143 12/20/2024
16.0.4 161 12/18/2024
16.0.3 145 12/18/2024
16.0.0 172 12/18/2024
15.3.4 491 12/7/2024
15.3.3 150 12/7/2024
15.3.2 157 12/7/2024
15.3.1 153 12/7/2024
15.3.0 165 12/6/2024
15.2.4 172 12/6/2024
15.2.3 171 12/6/2024
15.1.7 289 7/15/2024
15.1.6 222 6/11/2024
15.1.5 236 3/26/2024
15.1.4 177 3/26/2024
15.1.3 264 3/11/2024
15.1.2 196 3/8/2024
15.1.1 225 3/4/2024
15.1.0 204 2/28/2024
15.0.1 177 2/22/2024
15.0.0 226 2/20/2024
14.0.5 205 2/19/2024
14.0.4 144 2/19/2024
14.0.3 159 2/19/2024
14.0.2 187 2/15/2024
14.0.0 182 2/15/2024
14.0.0-alpha.30 95 2/15/2024
14.0.0-alpha.29 83 2/15/2024
14.0.0-alpha.28 90 2/15/2024
14.0.0-alpha.27 96 2/15/2024
14.0.0-alpha.26 98 2/15/2024
14.0.0-alpha.25 99 2/15/2024
14.0.0-alpha.24 98 2/15/2024
14.0.0-alpha.22 88 2/15/2024
14.0.0-alpha.20 102 2/15/2024
13.0.0 281 1/8/2024
12.1.2 211 1/6/2024
12.1.1 192 1/6/2024
12.1.0 183 1/6/2024
12.0.9 225 1/5/2024
12.0.8 214 1/5/2024
11.0.0 216 12/16/2023
8.0.3 188 12/11/2023
8.0.2 205 12/11/2023
8.0.0 232 12/11/2023