I'm using MVVM viewmodel-first pattern in a WPF application and I have some definitions like these:
public interface IMyView { }
[Export(typeof(IMyView))]
public class MyView : UserControl, IMyView { }
[ViewTypeAttribute(typeof(IMyView))]
public interface IMyViewModel { }
[Export(typeof(IMyViewModel))]
public class MyViewModel : ViewModelBase, IMyViewModel { }
That the ViewTypeAttribute
is a custom attribute to retrive which View
should be used as DataTemplate
for the given ViewModel
. Really here I have a Type
! But I don't know how to create an instance from that Type
via MEF
? can anybody help me please?
use:
var container = new CompositionContainer(/* your container .ctor here */);
var type = typeof (IYourType); // read the type from attribute
var export = container.GetExports(type, null, null).FirstOrDefault();
var obj = export.Value as YourCostingHere;