C #: Memperkenalkan Generator Kode Sumber

gambar


Kami dengan senang hati menyajikan kepada Anda pratinjau generator kode sumber. Ini adalah fitur baru yang memungkinkan pengembang C # untuk menganalisis kode khusus dan membuat file C # baru, yang pada gilirannya dapat ditambahkan ke proses kompilasi. Ini terjadi dengan bantuan komponen baru - Generator Sumber.


Untuk memulai dengan generator, Anda memerlukan pratinjau .NET 5 dan Visual Studio terbaru . Catatan: Visual Studio diperlukan untuk membangun generator kode sumber. Ini akan diubah pada pratinjau berikutnya dari .NET 5.


Apa itu generator kode sumber?


Generator kode sumber adalah bagian dari kode yang dieksekusi selama kompilasi, memeriksa program dan membuat file tambahan, yang kemudian dikompilasi dengan sisa kode.


Berikut adalah dua fitur utama yang memungkinkan Anda untuk menerapkan:


  • Dapatkan objek kompilasi yang mewakili semua kode pengguna yang dikompilasi. Objek ini dapat dianalisis, dan Anda dapat menulis kode yang berfungsi dengan model sintaksis dan semantik dari kode terkompilasi, seperti halnya dengan penganalisa modern.
  • C# , . , .

. , , C# . Roslyn, , C#.


:


gambar


.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(); 
        //  Console.WriteLine("Hello World!")     
    }
}

, :


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)
        {
            // TODO -     
        }

        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,       -->
<ItemGroup>
<!--   ,    "" ProjectReference. 
   'OutputItemType'  'ReferenceOutputAssmbly'. -->
    <ProjectReference Include="path-to-sourcegenerator-project.csproj" 
                      OutputItemType="Analyzer"
                      ReferenceOutputAssembly="false" />
</ItemGroup>

Roslyn, .


Visual Studio , . , :


public class SomeClassInMyCode
{
    public void SomeMethodIHave()
    {
        HelloWorldGenerated.HelloWorld.SayHello(); 
        //  Console.WriteLine("Hello World!")     
    }
}

: 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 ?


Meskipun generator kode sumber tidak secara teknis fitur bahasa C #, mereka ada di pratinjau. Alih-alih memperkenalkan pengaturan baru hanya untuk mereka, kami memutuskan bahwa akan lebih mudah untuk menggunakan switch yang ada, yang memungkinkan Anda untuk melihat fitur bahasa dari kompiler C #.


All Articles