FCin FCin - 1 year ago 104
C# Question

Import from another assembly throws exception when using MEF

I have a WPF project that expects imports from other assemblies.

namespace DrawMe.Core.ViewModels
public class MenuBarViewModel : Conductor<IMenuBarItem>
public IMenuBarItem Item { get; set; }

public MenuBarViewModel()

namespace DrawMe.MenuBarItems.ViewModels
public class FileViewModel : Screen, IMenuBarItem
public string Name { get; set; }
public List<IMenuBarOption> Options { get; set; }


When I export
class I get error:

The export 'DrawMe.MenuBarItems.ViewModels.FileViewModel (ContractName="DrawMe.Api.Models.MenuBar.IMenuBarItem")' is not assignable to type 'DrawMe.Api.Models.MenuBar.IMenuBarItem'.

I don't understand how this is possible. Most information I found is about assembly versions not matching, but I have a very basic solution and every project has the same version.

I tried using
Lazy<IMenuBarItem> Item
, but that imports

Also I use
, so I can do imports from classes other than

public static readonly string ExtensionFolderPath = Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Extensions");
private static readonly DirectoryCatalog DirectoryCatalog = new DirectoryCatalog(ExtensionFolderPath);
private static readonly CompositionContainer Container = new CompositionContainer(DirectoryCatalog);

public static void Inject<T>(T obj) where T : class

And Bootstrapper

protected override IEnumerable<Assembly> SelectAssemblies()
var assemblies = Directory.GetFiles(MefInjector.ExtensionFolderPath, "*.dll", SearchOption.AllDirectories).Select(Assembly.LoadFrom).ToList();
return assemblies;

Any suggestions, what else I can try to fix this?

Answer Source

So, I guess I fixed it. To be honest I don't know what was wrong. I just removed my project and created a new one. I did everything exactly the same way and now it works. Maybe it was the issue with not matching assemblies, but I don't really know how this would be possible taking into account that I cleaned my folder many times...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download