tags:

views:

40

answers:

1

I've been trying to figure this for a few days now, with no luck.

I'm trying to use the [ImportMany] to import from a directory full of DLL's with exports of type IEditorSystem, that have custom metadate of type IEditorSystemMetadata. I'd like to get the metadata first, and push it out to some textboxs etc so the user can choose which EditorSystem to use, and when selected, load that system...

I've been following examples as best as I can, here's what I have so far.

[ImportMany]
public ObservableCollection<Lazy<IEditorSystem, IEditorSystemMetadata>> EditorSystemList

This is what it should be importing:

[Export(typeof(IEditorSystem))]
    [SignalSystemData("Very Very Long Name", "Short Name")]
    public class MyEditorSystem: IEditorSystem
    {
        public MyEditorSystem()
        {
        }
    }

and the startup:

AggregateCatalog Catalog = new AggregateCatalog(
                new DirectoryCatalog(@".\EditorSystems"),
                new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            CompositionContainer Container = new CompositionContainer(Catalog);
            Container.ComposeParts(this);

I can see in the Catalog.Parts both the MyEditorSystem and the viewmodel that has the ImportMany, but EditorSystemList never gets populated. I get no Composition errors.

I thought it might have something to do with the Lazy<>, so i tried

public ObservableCollection<IEditorSystem> EditorSystemList

No luck either.

The only complication I can think of is that I'm using Cinch, which uses MEFedMVVM, which also uses MEF. I don't think it interferes, but I'm not really sure.

I figure I'm doing it wrong, can anyone make sense of this?

Update:

Implement a new IComposer, with exactly the catalog you need.

ImportMany is still failing though, but only when I try to import metadata with it. The metadata is just a couple of strings, and as far as I am able to determine, follows the examples.

FINALLY found the cause: The implementations of IEditorSystem are in a seperate DLL, as noted before. However, any new builds of the dll are not copied to the output subdir of the main project. I had copied the first one manually, and forgot to add a post-build copy to the dll project. Oh well, learned a bunch of stuff about MEF, so not completely wasted days :)

A: 

Without seeing your code, I believe all you need to change is

public ObservableCollection<Lazy<IEditorSystem, IEditorSystemMetadata>> EditorSystemList  

should be

public IEnumerable<Lazy<IEditorSystem, IEditorSystemMetadata>> EditorSystemList;

Here is a sample:

class Program
{
    static void Main(string[] args)
    {
        var c = new Class1();
        var v = c.EditorSystemList;
        foreach (var lazy in v)
        {
            if (lazy.Metadata.LongName == "Very Very Long Name")
            {
                var v2 = lazy.Value;
                // v2 is the instance of MyEditorSystem
            }
        }
    }
}

public class Class1
{
    [ImportMany]
    public IEnumerable<Lazy<IEditorSystem, IEditorSystemMetadata>> EditorSystemList;

    public Class1()
    {
        var catalog = new AggregateCatalog(
            new AssemblyCatalog(Assembly.GetExecutingAssembly()));
        var container = new CompositionContainer(catalog);
        container.ComposeParts(this);
    }
}

[Export(typeof(IEditorSystem))]
[SignalSystemData("Very Very Long Name", "Short Name")]
public class MyEditorSystem : IEditorSystem { }

public interface IEditorSystem { }

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class SignalSystemDataAttribute : ExportAttribute
{
    public SignalSystemDataAttribute(string longName, string shortName)
        : base(typeof(IEditorSystem))
    {
        LongName = longName;
        ShortName = shortName;
    }
    public string LongName { get; set; }
    public string ShortName { get; set; }
}

public interface IEditorSystemMetadata
{
    string LongName { get; }
    string ShortName { get; }
}
JoshVarga
I changed it to IEnumerable, but the import never seems to get done, EditorSystemList remains at [0]...
Kage
Did you try running the exact code from above? you should be able to create a Console app to test that.
JoshVarga
Yes, the Console version works, but it looks like it might be due to to MEFedMVVM messing things up, will update
Kage