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, IEditorSystemMe开发者_Go百科tadata>> 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 :)
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; }
}
Maybe my solution solves your problem too.
I was working hard trying to discover the problem.
then I got the following solution:
Metadata interface should contain only one property of the same type:
int, bool, string, etc. If you put two properties of Int, for example, the ImportMany> will not work and it will always return 0.
for each property of the interface, you must put the ExportMetadata Attribute at the exported class to.
for example,
public interface IMyExportMetadata { int a {get;} string b {get; } bool c {get;} }
[Export(typeof(IMyInterface)) [ExportMetadata("a", 0)] [ExportMetadata("b", "string")] [ExportMetadata("c", true)] public class myExportedClass: IMyInterface { }
to work with multiple booleans, for example, you must create a custom export attribute implementing the metadata interface like this:
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class ExportUserPartAttribute : ExportAttribute, IUserPartMetadata
{
#region Implementation of IUserPartMetadata
public int TipoPart { get; set; }
public string Regiao { get; set; }
public bool IsLogin { get; set; }
public bool IsMenu { get; set; }
public bool IsHome { get; set; }
public bool IsListagem { get; set; }
public bool IsFormulario { get; set; }
#endregion
public ExportUserPartAttribute()
: base(typeof(IUserPart))
{
}
/*
public ExportUserPartAttribute(int tipoPart, string regiao)
: base(typeof(IUserPart))
{
this.TipoPart = tipoPart;
this.Regiao = regiao;
}
*/
}
Maybe my solution solves your problem too.
I was working hard trying to discover the problem.
then I got the following solution:
Metadata interface should contain only one property of the same type:
int
, bool
, string
, etc. If you put two properties of int
, for example, the ImportMany<Lazy<t,m>>
will not work and it will always return 0.
for each property of the interface, you must put the ExportMetadata
Attribute at the exported class to.
for example,
public interface IMyExportMetadata
{
int a {get;}
string b {get;}
bool c {get;}
}
[Export(typeof(IMyInterface))
[ExportMetadata("a", 0)]
[ExportMetadata("b", "string")]
[ExportMetadata("c", true)]
public class myExportedClass: IMyInterface
{
}
精彩评论