using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection;
class Program { [Import()] public string ImportMessage { set; get; } }
class clsMySimpleMessage { [Export()] string ExportMessage { set { } get { return "Message inserted via MEF"; } }}
private void Compose() { // Get a reference to the current assemblies in the project. AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());// Once you have got the reference of catalog add it to the composition container. CompositionContainer compositionContainer = new CompositionContainer(catalog);// Finally call composeparts to connect the Import with Export. compositionContainer.ComposeParts(this);// use the property Console.WriteLine(ImportMessage); Console.ReadLine(); }
interface ImyInterface { string getString(); }
[Export(typeof(ImyInterface))] class clsMyImplementation : ImyInterface { public string getString() { return "This is my string"; } }
class Program { [Import()] public ImyInterface myInterface { set; get; } }
public partial class MainPage : UserControl { [ImportMany] public ObservableCollection<UserControl> ImportedItems { get; set; } }
[Export(typeof(UserControl))] public partial class SilverlightControl1 : UserControl { public SilverlightControl1() { InitializeComponent(); } }[Export(typeof(UserControl))] public partial class SilverlightControl2 : UserControl { public SilverlightControl2() { InitializeComponent(); } }
CompositionInitializer.SatisfyImports(this);
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: