RhoMicro.AttributeFactoryGenerator 2.0.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package RhoMicro.AttributeFactoryGenerator --version 2.0.0
NuGet\Install-Package RhoMicro.AttributeFactoryGenerator -Version 2.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.AttributeFactoryGenerator" Version="2.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.
paket add RhoMicro.AttributeFactoryGenerator --version 2.0.0
#r "nuget: RhoMicro.AttributeFactoryGenerator, 2.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.
// Install RhoMicro.AttributeFactoryGenerator as a Cake Addin
#addin nuget:?package=RhoMicro.AttributeFactoryGenerator&version=2.0.0

// Install RhoMicro.AttributeFactoryGenerator as a Cake Tool
#tool nuget:?package=RhoMicro.AttributeFactoryGenerator&version=2.0.0

RhoMicro.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.Generators.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