An open API service indexing awesome lists of open source software.

https://github.com/jscarle/attributesourcegenerator

A simple attribute-based Roslyn incremental source generator base class for .NET.
https://github.com/jscarle/attributesourcegenerator

attribute attribute-based charp dotnet source-generator source-generators

Last synced: 3 months ago
JSON representation

A simple attribute-based Roslyn incremental source generator base class for .NET.

Awesome Lists containing this project

README

          

# AttributeSourceGenerator

A simple attribute-based Roslyn incremental source generator base class for .NET.

[![main](https://img.shields.io/github/actions/workflow/status/jscarle/AttributeSourceGenerator/main.yml?logo=github)](https://github.com/jscarle/AttributeSourceGenerator)
[![nuget](https://img.shields.io/nuget/v/AttributeSourceGenerator)](https://www.nuget.org/packages/AttributeSourceGenerator)
[![downloads](https://img.shields.io/nuget/dt/AttributeSourceGenerator)](https://www.nuget.org/packages/AttributeSourceGenerator)

### Example generator

```csharp
using AttributeSourceGenerator;
using Microsoft.CodeAnalysis;

namespace SourceGenerators;

[Generator]
public sealed class IdentifierSourceGenerator : AttributeIncrementalGeneratorBase
{
public IdentifierSourceGenerator()
: base(() => new AttributeIncrementalGeneratorConfiguration()
{
MarkerAttributeName = MarkerAttributeName,
MarkerAttributeSource = MarkerAttributeSource,
SymbolFilter = FilterType.Struct,
SourceGenerator = GenerateIdentifier
})
{
}

private const string MarkerAttributeNamespace = "Domain.Common.Attributes";

private const string MarkerAttributeName = $"{MarkerAttributeNamespace}.GeneratedIdentifierAttribute`1";

private static source MarkerAttributeSource = new Source("GeneratedIdentifierAttribute`1", $$"""
namespace {{MarkerAttributeNamespace}};

[AttributeUsage(AttributeTargets.Struct)]
public sealed class GeneratedIdentifierAttribute : Attribute;
""";

private static IEnumerable GenerateIdentifier(Symbol symbol)
{
return [new Source(symbol.Name, $$"""
//

#nullable enable

namespace {{symbol.Namespace}};

partial struct {{symbol.Name}} : IEquatable<{{symbol.Name}}>, IComparable<{{symbol.Name}}>, IComparable
{
// Implementation details
}
""")];
}
}
```

### Typical .csproj

```xml


netstandard2.0
enable
true
latest
latest-All
true
true
true





true
false
true
false
$(NoWarn);NU5128
$(GetTargetPathDependsOn);GetDependencyTargetPaths









```