forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added snippets, images, notes, clarifying statements, clean up, etc. (d…
…otnet#27594) * Added snippets, images, notes, clarifying statments, clean up, etc. * A few tweaks and modifications * Update docs/csharp/roslyn-sdk/source-generators-overview.md Co-authored-by: Andy (Steve) De George <[email protected]> Co-authored-by: Andy (Steve) De George <[email protected]>
- Loading branch information
1 parent
9eff22e
commit 6bf4df1
Showing
10 changed files
with
262 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+101 KB
docs/csharp/roslyn-sdk/media/source-generators/solution-explorer-program.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+88.7 KB
docs/csharp/roslyn-sdk/media/source-generators/source-generated-program.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
docs/csharp/roslyn-sdk/snippets/source-generators/ConsoleApp/ConsoleApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SourceGenerator\SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> | ||
</ItemGroup> | ||
|
||
</Project> |
11 changes: 11 additions & 0 deletions
11
docs/csharp/roslyn-sdk/snippets/source-generators/ConsoleApp/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace ConsoleApp; | ||
|
||
partial class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
HelloFrom("Generated Code"); | ||
} | ||
|
||
static partial void HelloFrom(string name); | ||
} |
37 changes: 37 additions & 0 deletions
37
docs/csharp/roslyn-sdk/snippets/source-generators/SourceGenerator/HelloSourceGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace SourceGenerator | ||
{ | ||
[Generator] | ||
public class HelloSourceGenerator : ISourceGenerator | ||
{ | ||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
// Find the main method | ||
var mainMethod = context.Compilation.GetEntryPoint(context.CancellationToken); | ||
|
||
// Build up the source code | ||
string source = $@" // Auto-generated code | ||
using System; | ||
namespace {mainMethod.ContainingNamespace.ToDisplayString()} | ||
{{ | ||
public static partial class {mainMethod.ContainingType.Name} | ||
{{ | ||
static partial void HelloFrom(string name) => | ||
Console.WriteLine($""Generator says: Hi from '{{name}}'""); | ||
}} | ||
}} | ||
"; | ||
var typeName = mainMethod.ContainingType.Name; | ||
|
||
// Add the source code to the compilation | ||
context.AddSource($"{typeName}.g.cs", source); | ||
} | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
// No initialization required for this one | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
docs/csharp/roslyn-sdk/snippets/source-generators/SourceGenerator/SourceGenerator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
</Project> |
31 changes: 31 additions & 0 deletions
31
docs/csharp/roslyn-sdk/snippets/source-generators/source-generators.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.32002.185 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{B0A361D0-D9D7-4E39-AFC1-189121AAC9CE}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator", "SourceGenerator\SourceGenerator.csproj", "{7CB852F5-F320-41AF-B1AC-4CDBD61FEB17}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B0A361D0-D9D7-4E39-AFC1-189121AAC9CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B0A361D0-D9D7-4E39-AFC1-189121AAC9CE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B0A361D0-D9D7-4E39-AFC1-189121AAC9CE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B0A361D0-D9D7-4E39-AFC1-189121AAC9CE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{7CB852F5-F320-41AF-B1AC-4CDBD61FEB17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{7CB852F5-F320-41AF-B1AC-4CDBD61FEB17}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{7CB852F5-F320-41AF-B1AC-4CDBD61FEB17}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{7CB852F5-F320-41AF-B1AC-4CDBD61FEB17}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {8483CBEE-3CD5-4416-96BF-D457893183B6} | ||
EndGlobalSection | ||
EndGlobal |
Oops, something went wrong.