Skip to content

Commit 44e3949

Browse files
committed
Target .netstandard 2.0
1 parent 99e6288 commit 44e3949

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

SourceGen/MySourceGenerator.cs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@
33
using Microsoft.CodeAnalysis.Text;
44
using System.Text;
55

6-
namespace SourceGen;
7-
8-
[Generator]
9-
public class MySourceGenerator : IIncrementalGenerator
6+
namespace SourceGen
107
{
11-
public void Initialize(IncrementalGeneratorInitializationContext context)
8+
[Generator]
9+
public class MySourceGenerator : IIncrementalGenerator
1210
{
13-
// Define the attribute
14-
context.RegisterPostInitializationOutput(ctx =>
11+
public void Initialize(IncrementalGeneratorInitializationContext context)
1512
{
16-
var attributeSource = @"// <auto-generated/>
13+
// Define the attribute
14+
context.RegisterPostInitializationOutput(ctx =>
15+
{
16+
var attributeSource = @"// <auto-generated/>
1717
namespace SourceGenAttribute
1818
{
1919
[System.AttributeUsage(System.AttributeTargets.Class)]
2020
public class GenStuffAttribute : System.Attribute
2121
{
2222
}
2323
}";
24-
ctx.AddSource("GenStuffAttribute.g.cs", SourceText.From(attributeSource, Encoding.UTF8));
25-
});
24+
ctx.AddSource("GenStuffAttribute.g.cs", SourceText.From(attributeSource, Encoding.UTF8));
25+
});
2626

27-
// Find classes with the attribute
28-
IncrementalValuesProvider<INamedTypeSymbol> classDeclarations = context.SyntaxProvider
29-
.ForAttributeWithMetadataName(
30-
"SourceGenAttribute.GenStuffAttribute",
31-
(node, _) => node is ClassDeclarationSyntax,
32-
(generatorAttributeSyntaxContext, _) => (INamedTypeSymbol)generatorAttributeSyntaxContext.TargetSymbol);
27+
// Find classes with the attribute
28+
IncrementalValuesProvider<INamedTypeSymbol> classDeclarations = context.SyntaxProvider
29+
.ForAttributeWithMetadataName(
30+
"SourceGenAttribute.GenStuffAttribute",
31+
(node, _) => node is ClassDeclarationSyntax,
32+
(generatorAttributeSyntaxContext, _) => (INamedTypeSymbol)generatorAttributeSyntaxContext.TargetSymbol);
3333

34-
// Generate extension methods
35-
context.RegisterSourceOutput(classDeclarations, (spc, classSymbol) =>
36-
{
37-
if (classSymbol.ContainingNamespace.IsGlobalNamespace)
34+
// Generate extension methods
35+
context.RegisterSourceOutput(classDeclarations, (spc, classSymbol) =>
3836
{
39-
return;
40-
}
41-
var className = classSymbol.Name;
42-
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
37+
if (classSymbol.ContainingNamespace.IsGlobalNamespace)
38+
{
39+
return;
40+
}
41+
var className = classSymbol.Name;
42+
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
4343

44-
var source = $@"// <auto-generated/>
44+
var source = $@"// <auto-generated/>
4545
namespace {namespaceName}
4646
{{
4747
public static class {className}Extensions
@@ -53,7 +53,8 @@ public static string GeneratedMethod(this {className} obj)
5353
}}
5454
}}";
5555

56-
spc.AddSource($"{className}Extensions.g.cs", SourceText.From(source, Encoding.UTF8));
57-
});
56+
spc.AddSource($"{className}Extensions.g.cs", SourceText.From(source, Encoding.UTF8));
57+
});
58+
}
5859
}
59-
}
60+
}

SourceGen/SourceGen.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
76
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
87
<IsRoslynComponent>true</IsRoslynComponent>
98
<PackageId>SourceGenTest</PackageId>

0 commit comments

Comments
 (0)