
我们很高兴为您呈现源代码生成器的预览。这是一项新功能,允许C#开发人员分析自定义代码并创建新的C#文件,然后可以将其添加到编译过程中。这是在新组件-Source Generator的帮助下发生的。
要开始使用生成器,您将需要最新的.NET 5预览版和Visual Studio预览版。注意:需要Visual Studio来构建源代码生成器。这将在.NET 5的下一个预览版中进行更改。
什么是源代码生成器?
源代码生成器是一段在编译过程中执行,检查程序并创建其他文件的代码,然后与其他代码一起编译。
这是您可以实现的两个主要功能:
- 获取一个表示所有已编译用户代码的编译对象。可以分析此对象,并且您可以编写与已编译代码的句法和语义模型一起使用的代码,就像现代分析器一样。
- 生成可在编译期间添加到编译对象的源C#文件。换句话说,您可以在编译过程中将源代码添加为编译输入。
这两个功能使生成器非常有用。您可以使用在编译时生成的所有元数据来解析自定义代码,然后将C#代码发送回编译。如果您熟悉Roslyn分析器,则可以将源代码生成器理解为可以生成C#源代码的分析器。
生成器作为编译阶段运行:

源代码生成器是.NET Standard 2.0程序集,由编译器与分析器一起加载,并且可以在可用.NET Standard组件的环境中使用。
, .
, .
— , , , .
: , IL (IL weaving) MSBuild. .
— .NET. . .
, ASP.NET Core - , , razor . , : - , , , , . , .
— . .
, . MSBuild C# ( CSC) . , . MSBuild, , .
, — "-" (“stringly-typed”) API. , , ASP.NET Core razor. . , , .
API . -, .
Ahead-of-Time (AOT)
linker-based AOT-. , System.Text.Json, System.Text.RegularExpressions; ASP.NET Core WPF, / .
NuGet . .NET, "" ( “linkability”) AOT . OSS , .NET.
Hello World-
, .
, “Hello World” , . :
public class SomeClassInMyCode
{
public void SomeMethodIHave()
{
HelloWorldGenerated.HelloWorld.SayHello();
}
}
, :
1. .NET.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<PropertyGroup>
<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json ;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0-3.20207.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0-beta2.final" PrivateAssets="all" />
</ItemGroup>
</Project>
2. C#, .
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace MyGenerator
{
[Generator]
public class MySourceGenerator : ISourceGenerator
{
public void Execute(SourceGeneratorContext context)
{
}
public void Initialize(InitializationContext context)
{
}
}
}
Microsoft.CodeAnalysis.Generator Microsoft.CodeAnalysis.ISourceGenerator.
3. .
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace SourceGeneratorSamples
{
[Generator]
public class HelloWorldGenerator : ISourceGenerator
{
public void Execute(SourceGeneratorContext context)
{
var sourceBuilder = new StringBuilder(@"
using System;
namespace HelloWorldGenerated
{
public static class HelloWorld
{
public static void SayHello()
{
Console.WriteLine("" !"");
Console.WriteLine("" "");
");
var syntaxTrees = context.Compilation.SyntaxTrees;
foreach (SyntaxTree tree in syntaxTrees)
{
sourceBuilder.AppendLine($@"Console.WriteLine(@"" - {tree.FilePath}"");");
}
sourceBuilder.Append(@"
}
}
}");
context.AddSource("helloWorldGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
}
public void Initialize(InitializationContext context)
{
}
}
}
4. LangVersion .
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="path-to-sourcegenerator-project.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
Roslyn, .
Visual Studio , . , :
public class SomeClassInMyCode
{
public void SomeMethodIHave()
{
HelloWorldGenerated.HelloWorld.SayHello();
}
}
: Visual Studio, IntelliSense
:
- , , INotifyPropertyChanged,
- , SourceGeneratorContext,
- JSON- . .
Source Generators Cookbook.
GitHub.
, — , IntelliSense , Visual Studio.
. — . API . C# 9, API , .
C# : ! .NET , C# – . , - .
:
, .
Visual Studio , “ 1.0”. , . .NET 5 . , API, - OSS.
. , . .
FAQ
, . .
— , , , . , . , , . , - , .
F#?
F#. . , F# , . — , C#, , C# .
?
. — . “”, . .
?
, . , , . C#, . -.
/ ?
. , / . , C#.
?
C# 9. , .
TFM ?
. .NET Standard 2.0 TFM . .NET Standard 2.0.
Visual Basic F#?
#. , . VB . F# — .
?
, . VB F# , , . . . , , .
Intellisense ? Visual Studio , ?
Visual Studio. . Visual Studio .
Visual Studio?
Visual Studio.
?
NuGet, , . .
Microsoft ?
. . , Microsoft .
LangVersion ?
尽管从技术上讲源代码生成器不是C#语言功能,但它们在预览中。我们决定不再使用仅针对它们的新设置,而是使用现有的开关会更容易,这使您可以查看C#编译器的语言功能。