Skip to content

ES CSharp Directive Reference

Codex edited this page Oct 11, 2026 · 2 revisions

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

Conectando el conjunto a .csx

Manual C# · Guía Práctica

La directiva script agrega un ensamblado administrado listo para usar para la compilación de scripts. La biblioteca debe existir en la ruta especificada.

Ejemplo sencillo (Example.csx)

Guarde este ejemplo por separado como Example.csx. Ejecútelo independientemente de los demás ejemplos de esta página.

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 .

Resultado:

7

Sintaxis

#r "path/Library.dll"

Primero, guarde los archivos de la biblioteca enumerados y ejecute una compilación dotnet para ellos .csproj. Se proporcionan #r rutas para Debug/net10.0. Para NuGet o un conjunto de dependencias, es más conveniente utilizar inmediatamente el escenario .csproj con PackageReference.

Biblioteca de funciones matemáticas.

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

Archivo ApiLibrary/ApiLibrary.csproj:

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

Archivo ApiLibrary/Tools.cs:

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

Biblioteca de modelos propios

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

Archivo Models/Models.csproj:

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

Archivo Models/Point.cs:

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

Fuente oficial: Microsoft Aprender.

Clone this wiki locally