Dartk.CSharp.SourceGen.Razor 0.2.1

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Dartk.CSharp.SourceGen.Razor --version 0.2.1
NuGet\Install-Package Dartk.CSharp.SourceGen.Razor -Version 0.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dartk.CSharp.SourceGen.Razor" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dartk.CSharp.SourceGen.Razor --version 0.2.1
#r "nuget: Dartk.CSharp.SourceGen.Razor, 0.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dartk.CSharp.SourceGen.Razor as a Cake Addin
#addin nuget:?package=Dartk.CSharp.SourceGen.Razor&version=0.2.1

// Install Dartk.CSharp.SourceGen.Razor as a Cake Tool
#tool nuget:?package=Dartk.CSharp.SourceGen.Razor&version=0.2.1

CSharp.SourceGen.Razor

A C# source generator that renders Razor templates.

Warning: The source generator requires .NET 7 installed and dotnet command available. More details in Source generation section.

Info: If the project targets .NET Standard 2.0, then the project's references can be used in Razor templates.

Installation

dotnet add package Dartk.CSharp.SourceGen.Razor

To avoid propagating dependency on the package set the option PrivateAssets="all" in the project file:


<ItemGroup>
    <PackageReference Include="Dartk.CSharp.SourceGen.Razor" Version="0.2.1" PrivateAssets="All" />
</ItemGroup>

Include razor template files with .razor extension to the project as AdditionalFiles. For example, to render all razor templates in the project add this to the project file:


<ItemGroup>
    <AdditionalFiles Include="**/*.razor" />
</ItemGroup>

A complete example is presented below.

Source generation

Razor engine does not render a template directly, instead it generates a C# class that has a method that returns a rendered output. In order to render a template, an intermediate library with Razor generated classes needs to be compiled.

The source generator passes all found .razor (included to the project as AdditionalFiles) templates to the Razor engine, which generates C# classes that render templates. Those classes are compiled into a temporary intermediate library.

Source generators target .NET Standard 2.0 which does not support assembly unloading. In order to prevent memory leak by repeated assembly loading, the intermediate library is called in a separate process by an external .NET 7 executable.

If the project that uses the source generator targets .NET Standard 2.0, then the project's references will be referenced by the intermediate library.

Saving generated files

To save the generated source files set properties EmitCompilerGeneratedFiles and CompilerGeneratedFilesOutputPath in the project file:


<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    
    <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles
    </CompilerGeneratedFilesOutputPath>
</PropertyGroup>

Example

Create a new console C# project:

dotnet new Example.Netstandard2_0

Install the package Dartk.CSharp.SourceGen.Razor and set the property PrivateAssets="All" by editing the project file Example.csproj:


<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        
        <PackageReference Include="Dartk.CSharp.SourceGen.Razor" Version="0.2.1"
                          PrivateAssets="All" />

        
        <PackageReference Include="Scriban" Version="5.7.0" />
    </ItemGroup>

    
    <ItemGroup>
        <AdditionalFiles Include="**\*.razor" />
    </ItemGroup>
</Project>

Create a file RazorScribanMadness.razor:

namespace Example.Netstandard2_0;

// 'partial' is used for JetBrains Rider, it's linter thinks that a '.razor' file declares
// a class, and will treat the generated code as second declaration, highlighting errors
// in places where the class is being used.
public static partial class RazorScribanMadness
{
    @using Scriban
    @{
        @:public static string Why() => "@(RenderScriban())";

        string RenderScriban()
        {
            var template = Template.Parse("Because {{ reason }}!");
            return template.Render(new { reason = "you can" });
        }
    }
}

The template above will generate following code:

namespace Example.Netstandard2_0;

// 'partial' is used for JetBrains Rider, it's linter thinks that a '.razor' file declares
// a class, and will treat the generated code as second declaration, highlighting errors
// in places where the class is being used.
public static partial class RazorScribanMadness
{
        public static string Why() => "Because you can!";
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated