Skip to content

Compiler

James Courtney edited this page Jun 12, 2021 · 13 revisions

FlatSharp can be run at build-time with the FlatSharp.Compiler NuGet package. The compiler accepts fbs files and produces C# at build time.

Adding FBS files

FBS files must be opted into the compiler:

<Project Sdk="Microsoft.NET.Sdk">
  <ItemGroup>
    <FlatSharpSchema Include="YourSchema.fbs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="FlatSharp.Compiler" Version="5.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="FlatSharp.Runtime" Version="5.0.0" />
  </ItemGroup>
</Project>

Unity

Unity does not allow modification of the .csproj file, which makes the above difficult. For Unity users, the best current option is to invoke the compiler manually in a prebuild step. The syntax is:

  • net47 (Windows only): FlatSharp.Compiler.exe -i <input_file> -o <output directory>
  • net5.0 (CrossPlatform): dotnet FlatSharp.Compiler.dll -i <input_file> -o <output directory>

Nullable Annotations

By default, the FlatSharp compiler inherits the project setting for nullable annotations. If <Nullable> is set to enabled in the project, then FlatSharp will generate code with full nullable attributes and annotations. If <Nullable> is set to anything besides enabled in the project, FlatSharp will generate code with nullable annotations only.

Full nullable annotations may only work correctly with C# 9.

This behavior can be overridden with the <FlatSharpNullable> property.

  <!-- Flatsharp inherits the project setting and generates full nullable code -->
  <PropertyGroup>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <!-- FlatSharp only generates nullable annotations (no attributes/warnings) -->
  <PropertyGroup>
    <Nullable>enable</Nullable>
    <FlatSharpNullable>false</FlatSharpNullable>
  </PropertyGroup>

  <!-- FlatSharp only generates nullable annotations (no attributes/warnings) -->
  <PropertyGroup>
    <Nullable>disable</Nullable>
  </PropertyGroup>

  <!-- Flatsharp overrides the project setting and generates full nullable code -->
  <PropertyGroup>
    <Nullable>disable</Nullable>
    <FlatSharpNullable>true</FlatSharpNullable>
  </PropertyGroup>
Clone this wiki locally