Repository navigation
RU CSharp Keyword alias
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Справочник C# · Практическое руководство
Псевдоним внешнего сборника добавляет отдельное корневое пространство имен. Он позволяет различать одинаковые полные названия типов из разных сборников. Aliases задается для ссылки в проекте; в коде псевдоним объявляют через extern alias и используют с оператором ::.
Сохраните все приведённые файлы рядом и запускайте Example.csproj. В отдельном Example.cs ссылка с псевдонимом Tools не настроена.
Example.cs:
// PROJECT-EXAMPLE
extern alias Tools;
System.Console.WriteLine(Tools::Demo.Number.Value);Dependency.cs:
// PROJECT-EXAMPLE
namespace Demo; public static class Number { public static int Value => 7; }Dependency.csproj:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework><EnableDefaultCompileItems>false</EnableDefaultCompileItems></PropertyGroup><ItemGroup><Compile Include="Dependency.cs" /></ItemGroup></Project>Example.csproj:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net10.0</TargetFramework><EnableDefaultCompileItems>false</EnableDefaultCompileItems></PropertyGroup><ItemGroup><Compile Include="Example.cs" /><ProjectReference Include="Dependency.csproj"><Aliases>Tools</Aliases></ProjectReference></ItemGroup></Project>dotnet run --project Example.csprojРезультат:
7
extern alias Legacy;
using OldLabel = Legacy::Shared.Label;Файл Example.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Dependency/**/*.cs" />
<ProjectReference Include="Dependency/Dependency.csproj"><Aliases>Legacy</Aliases></ProjectReference>
</ItemGroup>
</Project>Файл Program.cs:
extern alias Legacy;
using System;
internal static class Program
{
public static void Main()
{
var label = new Legacy::Shared.Label("Local library");
Console.WriteLine(label.Text);
}
}Файл Dependency/Dependency.csproj:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>Файл Dependency/Label.cs:
namespace Shared;
public sealed class Label(string text)
{
public string Text { get; } = text;
}Результат:
Local library
Файл Example.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="LegacyDependency/**/*.cs;ModernDependency/**/*.cs" />
<ProjectReference Include="LegacyDependency/LegacyDependency.csproj"><Aliases>OldApi</Aliases></ProjectReference>
<ProjectReference Include="ModernDependency/ModernDependency.csproj"><Aliases>NewApi</Aliases></ProjectReference>
</ItemGroup>
</Project>Файл Program.cs:
extern alias OldApi;
extern alias NewApi;
using System;
using OldCounter = OldApi::Shared.Counter;
using NewCounter = NewApi::Shared.Counter;
internal static class Program
{
public static void Main()
{
Console.WriteLine($"Old: {OldCounter.Scale(5)}");
Console.WriteLine($"New: {NewCounter.Scale(5)}");
}
}Файл LegacyDependency/LegacyDependency.csproj:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>Файл LegacyDependency/Counter.cs:
namespace Shared;
public static class Counter { public static int Scale(int value) => value * 2; }Файл ModernDependency/ModernDependency.csproj:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>Файл ModernDependency/Counter.cs:
namespace Shared;
public static class Counter { public static int Scale(int value) => value * 3; }Результат:
Old: 10
New: 15
Официальный источник: Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE