Skip to content

KO CSharp Keyword alias

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: alias

핸드북 C# · 실용 가이드

외부 어셈블리 별칭은 별도의 루트 네임스페이스를 추가합니다. 이를 통해 서로 다른 어셈블리의 동일한 정규화된 유형 이름을 구별할 수 있습니다. 프로젝트의 참조를 위해 별칭이 지정됩니다. 코드에서 별칭은 extern 별칭을 통해 선언되고 :: 연산자와 함께 사용됩니다.

간단한 예제 (Example.cs)

표시된 모든 파일을 같은 폴더에 저장하고 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 학습.

Clone this wiki locally