성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - 네임스페이스까지 동일한 타입을 2개의 DLL에서 제공하는 경우 충돌을 우회하는 방법</h1> <p> 예를 들어 볼까요?<br /> <br /> 다음의 클래스를 포함하는 2개의 Class Library 프로젝트를 만들고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // <span style='color: blue; font-weight: bold'>ClassLibrary1</span> 프로젝트 namespace <span style='color: blue; font-weight: bold'>MyLibrary</span> { public class <span style='color: blue; font-weight: bold'>MyClass</span> { public static void <span style='color: blue; font-weight: bold'>Print</span>() { Console.WriteLine("ClassLibrary1: MyClass"); } } } </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // <span style='color: blue; font-weight: bold'>ClassLibrary2</span> 프로젝트 namespace <span style='color: blue; font-weight: bold'>MyLibrary</span> { public class <span style='color: blue; font-weight: bold'>MyClass</span> { public static void <span style='color: blue; font-weight: bold'>Output</span>() { Console.WriteLine("ClassLibrary2: MyClass"); } } } </pre> <br /> 콘솔 프로젝트를 만들어 위의 2개 dll(ClassLibrary1, ClassLibrary2)을 참조 추가합니다. 그런 다음 이렇게 코딩을 하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; internal class Program { static void Main(string[] args) { <span style='color: blue; font-weight: bold'>MyLibrary.MyClass</span>.Print(); } } </pre> <br /> 빌드 시 어떤 어셈블리에 대한 "타입"을 바인딩해야 할지 알 수 없어 컴파일 오류가 (당연히) 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Error CS0433 The type 'MyClass' exists in both 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' </pre> <br /> 바로 이런 경우에, 어느 한쪽의 네임스페이스 공간을 따로 별칭을 부여하는 것이 가능합니다. 따라서 위의 상황에서는 ClassLibrary1, ClassLibrary2 둘 중의 하나에 대해 csproj 프로젝트 파일에서 Aliases 태그를 부여해 한번 더 구분 가능한 식별자 역할을 추가할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" /> <ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj"> <span style='color: blue; font-weight: bold'><Aliases>Isolated</Aliases></span> </ProjectReference> </ItemGroup> </Project> </pre> <br /> 위의 예제에서는 ClassLibrary2에 대해 별칭을 지정했는데요, 그럼 아래와 같이 2개의 클래스를 모두 충돌 없이 사용할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <span style='color: blue; font-weight: bold'>extern alias Isolated;</span> using System; internal class Program { static void Main(string[] args) { <span style='color: blue; font-weight: bold'>MyLibrary.MyClass.Print(); Isolated.MyLibrary.MyClass.Output();</span> } } /* 출력 결과 ClassLibrary1: MyClass ClassLibrary2: MyClass */ </pre> <br /> 그런데, 사실 우리가 인식하지 못했을 뿐이지 네임스페이스에 대한 기본 별칭이 하나 있습니다. 바로 global입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > global - :: operator (C# reference) ; <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/namespace-alias-qualifier'>https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/namespace-alias-qualifier</a> </pre> <br /> 이런 관점에서 위의 코드를 다음과 같이 바꿀 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > extern alias Isolated; using System; internal class Program { static void Main(string[] args) { <span style='color: blue; font-weight: bold'>global::</span>MyLibrary.MyClass.Print(); <span style='color: blue; font-weight: bold'>Isolated::</span>MyLibrary.MyClass.Output(); } } /* 출력 결과 ClassLibrary1: MyClass ClassLibrary2: MyClass */ </pre> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1882&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
9171
(왼쪽의 숫자를 입력해야 합니다.)