Skip to content

Here are some common project examples for C# language.

For detailed C# target configuration, see Configure Targets and Project Target API.

Console Application

EXPLORER
src
Program.cs
xmake.lua
Lua xmake.lua
12345
add_rules("mode.debug", "mode.release")

target("hello")
    set_kind("binary")
    add_files("src/*.cs")

Shared Library

EXPLORER
src
app
Program.cs
lib
MyLib.cs
xmake.lua
Lua xmake.lua
12345678910
add_rules("mode.debug", "mode.release")

target("mylib")
    set_kind("shared")
    add_files("src/lib/*.cs")

target("app")
    set_kind("binary")
    add_deps("mylib")
    add_files("src/app/*.cs")

Use add_deps to automatically link a C# shared library to the executable.

NuGet Package Integration

EXPLORER
src
Program.cs
xmake.lua
Lua xmake.lua
12345678
add_requires("nuget::Humanizer.Core 2.14.1")

add_rules("mode.debug", "mode.release")

target("app")
    set_kind("binary")
    add_files("src/*.cs")
    add_packages("nuget::Humanizer.Core")

Integrate NuGet packages via add_requires("nuget::xxx"), using the same approach as C/C++ packages.

C# and C/C++ Interop (P/Invoke)

EXPLORER
src
app
Program.cs
native
mathlib.c
xmake.lua
Lua xmake.lua
12345678910
add_rules("mode.debug", "mode.release")

target("mathlib")
    set_kind("shared")
    add_files("src/native/*.c")

target("app")
    set_kind("binary")
    add_deps("mathlib")
    add_files("src/app/*.cs")

Xmake supports C# calling C/C++ native libraries via P/Invoke, and automatically handles native shared library search paths (PATH/LD_LIBRARY_PATH/DYLD_LIBRARY_PATH) without manual configuration.

ASP.NET Core Web Project

Set the SDK type via csharp.sdk to build ASP.NET Core web projects:

lua
target("webapp")
    set_kind("binary")
    add_values("csharp.sdk", "Microsoft.NET.Sdk.Web")
    add_files("src/Program.cs")

Configuration Options

The C# rule supports a wide range of .NET project properties via set_values(). These values are directly mapped to the corresponding properties in the .csproj file.

lua
target("app")
    set_kind("binary")
    add_files("src/*.cs")
    set_values("csharp.target_framework", "net8.0")
    set_values("csharp.lang_version", "12")
    set_values("csharp.nullable", "enable")
    set_values("csharp.allow_unsafe_blocks", true)
    set_values("csharp.publish_single_file", true)
    set_values("csharp.publish_trimmed", true)

Full Configuration Reference

Project

Config Name.csproj PropertyDescription
csharp.sdkProject SdkProject SDK type, default Microsoft.NET.Sdk, set to Microsoft.NET.Sdk.Web for web projects

General

Config Name.csproj PropertyDescription
csharp.target_frameworkTargetFrameworkTarget framework, e.g. net8.0, auto-detected from dotnet sdk if not set
csharp.target_frameworksTargetFrameworksMulti-target frameworks, e.g. {"net8.0", "net9.0"}, semicolon-joined
csharp.implicit_usingsImplicitUsingsImplicit usings, default enable
csharp.nullableNullableNullable reference types, default enable
csharp.lang_versionLangVersionLanguage version, e.g. 12.0, latest
csharp.root_namespaceRootNamespaceRoot namespace
csharp.enable_default_compile_itemsEnableDefaultCompileItemsEnable default compile items, default false
csharp.enable_default_embedded_resource_itemsEnableDefaultEmbeddedResourceItemsEnable default embedded resource items
csharp.enable_default_none_itemsEnableDefaultNoneItemsEnable default None items

Build

Config Name.csproj PropertyDescription
csharp.generate_assembly_infoGenerateAssemblyInfoGenerate assembly info
csharp.deterministicDeterministicDeterministic build
csharp.prefer_32bitPrefer32BitPrefer 32-bit
csharp.allow_unsafe_blocksAllowUnsafeBlocksAllow unsafe code blocks
csharp.check_for_overflow_underflowCheckForOverflowUnderflowCheck for arithmetic overflow

Code Analysis

Config Name.csproj PropertyDescription
csharp.analysis_levelAnalysisLevelAnalysis level
csharp.enable_net_analyzersEnableNETAnalyzersEnable .NET analyzers
csharp.enforce_code_style_in_buildEnforceCodeStyleInBuildEnforce code style in build
csharp.warnings_as_errorsWarningsAsErrorsTreat specified warnings as errors (list)
csharp.warnings_not_as_errorsWarningsNotAsErrorsExclude warnings from errors (list)
csharp.error_logErrorLogError log
csharp.generate_documentation_fileGenerateDocumentationFileGenerate documentation file
csharp.documentation_fileDocumentationFileDocumentation file path

Publish & Runtime

Config Name.csproj PropertyDescription
csharp.runtime_identifierRuntimeIdentifierRuntime identifier, e.g. win-x64, linux-arm64
csharp.runtime_identifiersRuntimeIdentifiersMultiple runtime identifiers (list)
csharp.self_containedSelfContainedSelf-contained deployment
csharp.use_app_hostUseAppHostUse app host
csharp.roll_forwardRollForwardRuntime roll-forward policy
csharp.publish_single_filePublishSingleFilePublish as single file
csharp.publish_trimmedPublishTrimmedTrim on publish
csharp.trim_modeTrimModeTrim mode
csharp.publish_ready_to_runPublishReadyToRunReadyToRun pre-compilation
csharp.invariant_globalizationInvariantGlobalizationInvariant globalization
csharp.include_native_libraries_for_self_extractIncludeNativeLibrariesForSelfExtractInclude native libraries for self-extract
csharp.enable_compression_in_single_fileEnableCompressionInSingleFileEnable compression in single file
csharp.publish_aotPublishAotAOT publish
csharp.strip_symbolsStripSymbolsStrip symbols
csharp.enable_trim_analyzerEnableTrimAnalyzerEnable trim analyzer
csharp.json_serializer_is_reflection_enabled_by_defaultJsonSerializerIsReflectionEnabledByDefaultJSON serializer reflection enabled by default
csharp.satellite_resource_languagesSatelliteResourceLanguagesSatellite resource languages (list)

Package Info

Config Name.csproj PropertyDescription
csharp.versionVersionPackage version
csharp.assembly_versionAssemblyVersionAssembly version
csharp.file_versionFileVersionFile version
csharp.informational_versionInformationalVersionInformational version
csharp.package_idPackageIdPackage ID
csharp.authorsAuthorsAuthors
csharp.companyCompanyCompany
csharp.productProductProduct name
csharp.descriptionDescriptionDescription
csharp.copyrightCopyrightCopyright
csharp.repository_urlRepositoryUrlRepository URL
csharp.repository_typeRepositoryTypeRepository type
csharp.package_license_expressionPackageLicenseExpressionPackage license expression
csharp.package_project_urlPackageProjectUrlPackage project URL
csharp.neutral_languageNeutralLanguageNeutral language
csharp.enable_preview_featuresEnablePreviewFeaturesEnable preview features

Output

Config Name.csproj PropertyDescription
csharp.generate_runtime_configuration_filesGenerateRuntimeConfigurationFilesGenerate runtime configuration files
csharp.copy_local_lock_file_assembliesCopyLocalLockFileAssembliesCopy local lock file assemblies
csharp.append_target_framework_to_output_pathAppendTargetFrameworkToOutputPathAppend target framework to output path, default false
csharp.append_runtime_identifier_to_output_pathAppendRuntimeIdentifierToOutputPathAppend runtime identifier to output path, default false
csharp.produce_reference_assemblyProduceReferenceAssemblyProduce reference assembly
csharp.disable_implicit_framework_referencesDisableImplicitFrameworkReferencesDisable implicit framework references
csharp.generate_target_framework_attributeGenerateTargetFrameworkAttributeGenerate target framework attribute

Custom Properties

Config NameDescription
csharp.propertiesAdd custom <PropertyGroup> entries, format: "Name=Value"
lua
set_values("csharp.properties", "MyCustomProp=value", "AnotherProp=value2")