Skip to content

DE CSharp Directive Reference

Codex edited this page Oct 11, 2026 · 2 revisions

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

Anschluss der Baugruppe an .csx

Handbuch C# · Praktischer Leitfaden

Die Skriptdirektive fügt eine vorgefertigte verwaltete Assembly für die Skriptkompilierung hinzu. Die Bibliothek muss im angegebenen Pfad vorhanden sein.

Einfaches Beispiel (Example.csx)

Speichern Sie dieses Beispiel separat als Example.csx. Führen Sie es unabhängig von den anderen Beispielen dieser Seite aus.

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 .

Ergebnis:

7

Syntax

#r "path/Library.dll"

Speichern Sie zunächst die aufgelisteten Bibliotheksdateien und führen Sie einen Dotnet-Build dafür aus .csproj. #r Pfade werden für Debug/net10.0 angegeben. Für NuGet oder eine Reihe von Abhängigkeiten ist es bequemer, das Szenario .csproj sofort mit PackageReference zu verwenden.

Bibliothek mathematischer Funktionen

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

Datei ApiLibrary/ApiLibrary.csproj:

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

Datei ApiLibrary/Tools.cs:

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

Bibliothek eigener Modelle

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

Datei Models/Models.csproj:

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

Datei Models/Point.cs:

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

Offizielle Quelle: Microsoft Learn.

Clone this wiki locally