Skip to content

FR CSharp Directive Reference

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Connexion de l'assemblage à .csx

Manuel C# · Guide pratique

La directive script ajoute un assembly géré prêt à l'emploi pour la compilation de scripts. La bibliothèque doit exister dans le chemin spécifié.

Exemple simple (Example.csx)

Enregistrez cet exemple séparément dans Example.csx. Exécutez-le indépendamment des autres exemples de cette page.

Example.csx:

#r "Library.dll"
UO.Print(Demo.Number.Value.ToString());

Library.cs:

// PROJECT-EXAMPLE
namespace Demo; public static class Number { public static int Value => 7; }

Library.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework><EnableDefaultCompileItems>false</EnableDefaultCompileItems></PropertyGroup><ItemGroup><Compile Include="Library.cs" /></ItemGroup></Project>
dotnet build Library.csproj -o .

Résultat:

7

Syntaxe

#r "path/Library.dll"

Tout d'abord, enregistrez les fichiers de bibliothèque répertoriés et exécutez une version dotnet pour ceux-ci .csproj. #r chemins sont donnés pour Debug/net10.0. Pour NuGet ou un ensemble de dépendances, il est plus pratique d'utiliser immédiatement le scénario .csproj avec PackageReference.

Bibliothèque de fonctions mathématiques

#r "ApiLibrary/bin/Debug/net10.0/ApiLibrary.dll"
UO.Print(Demo.Tools.Double(4).ToString());

Fichier ApiLibrary/ApiLibrary.csproj :

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>

Fichier ApiLibrary/Tools.cs :

namespace Demo; public static class Tools { public static int Double(int value) => value * 2; }

Bibliothèque de propres modèles

#r "Models/bin/Debug/net10.0/Models.dll"
Demo.Point point = new(2, 3);
UO.Print((point.X + point.Y).ToString());

Fichier Models/Models.csproj :

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>

Fichier Models/Point.cs :

namespace Demo; public readonly record struct Point(int X, int Y);

Source officielle : Microsoft Apprendre.

Clone this wiki locally