-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttributesGenerator.cs
48 lines (41 loc) · 1.17 KB
/
AttributesGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace SourceGenerators
{
[Generator]
public class AttributeGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
// No initialization required
}
public void Execute(GeneratorExecutionContext context)
{
// Generate the attribute source code
string attributeSource = @"
using System;
namespace IEnumerableUnpacker
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class UnpackableAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Field, Inherited = false)]
public class UnpackAttribute : Attribute
{
public string OutputName { get; }
public UnpackAttribute(string outputName)
{
OutputName = outputName;
}
}
}
";
// Add the generated source code to the compilation
context.AddSource("IEnumerableUnpackerGeneratedAttributes.cs", SourceText.From(attributeSource, Encoding.UTF8));
}
}
}