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.
- Host: GitHub
- URL: https://github.com/jscarle/attributesourcegenerator
- Owner: jscarle
- License: mit
- Archived: true
- Created: 2024-02-22T21:40:28.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-06T18:00:45.000Z (about 2 years ago)
- Last Synced: 2025-08-13T23:20:15.768Z (9 months ago)
- Topics: attribute, attribute-based, charp, dotnet, source-generator, source-generators
- Language: C#
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# AttributeSourceGenerator
A simple attribute-based Roslyn incremental source generator base class for .NET.
[](https://github.com/jscarle/AttributeSourceGenerator)
[](https://www.nuget.org/packages/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
```