티스토리 뷰
Querying the CompositionContainer
CompositionContainer 의 Exported 구성 요소를 취득하기 위해서는 컨테이너에 하나의 구성 요소만이 존재해야 합니다. 쿼리(Query) 를 통해 이러한 객체들이 여러 개 존재할 경우 MEF 는 예외를 발생하게 됩니다.
바로 아래와 같은 경우이죠.
l 하나의 인스턴스를 요청할 때, 인스턴스를 찾지 못했을 경우
l 하나의 인스턴스를 요청할 때, 인스턴스가 여러 개일 경우
GetExportedObject
일반적으로 ExportAttribute 에 인자가 전달되지 않은 경우는 클래스의 타입이 키 값이 되어, 아래와 같이 구성 요소를 취득할 수 있습니다.
class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var export = container.GetExportedObject<Export1>();
export.Say();
Console.ReadKey();
}
}
[Export]
class Export1
{
public void Say()
{
Console.WriteLine("Export1 Say..");
}
} |
만약, ExportAttribute 에 Contract Name 이 선언이 될 경우는 클래스의 타입 정보와 Contract Name 이 일치해야 구성요소를 취득할 수 있습니다.
class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var export = container.GetExportedObject<Export2>("export2");
export.Say();
Console.ReadKey();
}
}
[Export("export2")]
public class Export2
{
public void Say()
{
Console.WriteLine("Export2 Say..");
}
} |
GetExport
만약 여러 개의 구성 요소의 인스턴스가 필요하다면 GetExport 메서드를 통해 Export 의 구성 정보를 가져오면 됩니다.
var exports = container.GetExport<Export1>();
exports.GetExportedObject().Say(); |
필요하다면 ExportAttribute 의 Contract Name 으로 질의(Query) 할 수 있습니다.
var exports = container.GetExport<Export2>("export2");
exports.GetExportedObject().Say(); |
아쉽게도 MEF Preview 4 까지 지원하던 C# 3.0 의 Expression 을 통해 질의하는 방법은 MEF Preview 5 이후 없어진 것이 아쉽네요.
GetExportedObjectOrDefault
일반적으로 MEF 에서는 질의(Query) 결과가 없을 경우 예외를 발생하게 되는데, GetExportedObjectOrDefault 메서드를 통해 결과가 없을 경우 Null 값으로 대체할 수 있습니다.
var obj = container.GetExportedObjectOrDefault<Export1>("A");
if( obj != null )
obj.Say(); |
'.NET > .NET Framework' 카테고리의 다른 글
CodeFx 프로젝트 (0) | 2009.06.24 |
---|---|
MEF 세미나 자료 (0) | 2009.06.22 |
[MEF] 9. Recomposition (2) | 2009.04.20 |
[MEF] 8. Strongly Typed Metadata (0) | 2009.04.17 |
[MEF] 7. Exports and Metadata (1) | 2009.04.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- 2,833,299
- Today
- 2
- Yesterday
- 149
링크
- ***** MY SOCIAL *****
- [SOCIAL] 페이스북
- [SOCIAL] 팀 블로그 트위터
- .
- ***** MY OPEN SOURCE *****
- [GITHUB] POWERUMC
- .
- ***** MY PUBLISH *****
- [MSDN] e-Book 백서
- .
- ***** MY TOOLS *****
- [VSX] VSGesture for VS2005,200…
- [VSX] VSGesture for VS2010,201…
- [VSX] Comment Helper for VS200…
- [VSX] VSExplorer for VS2005,20…
- [VSX] VSCmd for VS2005,2008
- .
- ***** MY FAVORITES *****
- MSDN 포럼
- MSDN 라이브러리
- Mono Project
- STEN
- 일본 ATMARKIT
- C++ 빌더 포럼
- .
TAG
- ASP.NET
- .NET Framework 4.0
- c#
- 엄준일
- 팀 파운데이션 서버
- 비주얼 스튜디오
- TFS
- LINQ
- umc
- Visual Studio
- Silverlight
- TFS 2010
- 땡초
- .NET
- MEF
- github
- monodevelop
- ALM
- test
- Visual Studio 11
- Team Foundation Server 2010
- 비주얼 스튜디오 2010
- Visual Studio 2010
- mono
- Team Foundation Server
- Managed Extensibility Framework
- Visual Studio 2008
- testing
- POWERUMC
- Windows 8
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 |
글 보관함
- 2020/05 (1)
- 2019/10 (3)
- 2018/11 (1)
- 2018/08 (2)
- 2017/04 (1)
- 2017/01 (2)
- 2016/11 (2)
- 2016/08 (1)
- 2016/05 (1)
- 2016/04 (2)
- 2016/02 (2)
- 2016/01 (1)
- 2015/05 (1)
- 2015/04 (2)
- 2015/03 (1)
- 2015/02 (1)
- 2015/01 (1)
- 2014/11 (1)
- 2014/09 (2)
- 2014/08 (2)
- 2014/05 (2)
- 2014/04 (3)
- 2014/03 (2)
- 2014/02 (2)
- 2014/01 (4)
- 2013/12 (2)
- 2013/11 (1)
- 2013/10 (2)
- 2013/09 (6)
- 2013/08 (3)