dotnet.exeでプリプロセッサのシンボルを定義するには。.csproj編集

C# コンピュータ
C#

.csprojにDefineConstantsでシンボルを定義します。

csprojのサンプル

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <DefineConstants>ASKSA</DefineConstants>
  </PropertyGroup>

</Project>

csファイルのサンプル

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

#if ASKSA
Console.WriteLine("ASKSA");
#endif

csprojでASKSAを定義したことでcsファイルのConsole.WriteLine("ASKSA");が実行され文字が出力されます。

csprojで定義することでプロジェクト内にある全てのcsファイルで定義されたことに成るうようです。

コメント