Skip to content

Instantly share code, notes, and snippets.

@devlights
Created March 27, 2014 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devlights/9805246 to your computer and use it in GitHub Desktop.
Save devlights/9805246 to your computer and use it in GitHub Desktop.
MEF 属性ベースのサンプル
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