Created
March 27, 2014 11:12
-
-
Save devlights/9805246 to your computer and use it in GitHub Desktop.
MEF 属性ベースのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MEFSamples | |
{ | |
using System; | |
using System.Linq; | |
using System.ComponentModel.Composition; | |
using System.ComponentModel.Composition.Hosting; | |
class Program | |
{ | |
[Import("upper", typeof(ILog))] | |
public ILog Logger { get; set; } | |
static void Main(string[] args) | |
{ | |
var catalog = new AggregateCatalog(); | |
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); | |
var container = new CompositionContainer(catalog); | |
var obj = container.GetExportedValue<ILog>("reverse"); | |
obj.Debug("hello world"); | |
var me = new Program(); | |
container.ComposeParts(me); | |
me.Logger.Debug("hello world"); | |
foreach (var item in container.GetExportedValues<ILog>()) | |
{ | |
item.Debug("GetExportedValues"); | |
} | |
} | |
} | |
interface ILog | |
{ | |
void Debug(string message); | |
} | |
[Export(typeof(ILog))] | |
class LogImpl : ILog | |
{ | |
public void Debug(string message) | |
{ | |
Console.WriteLine(message); | |
} | |
} | |
[Export("upper", typeof(ILog))] | |
class LogUpperImpl : ILog | |
{ | |
public void Debug(string message) | |
{ | |
Console.WriteLine(message.ToUpper()); | |
} | |
} | |
[Export("reverse", typeof(ILog))] | |
class LogReverseImpl : ILog | |
{ | |
public void Debug(string message) | |
{ | |
Console.WriteLine(new string(message.Reverse().ToArray())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment