ImportMany with non-importing metadata

I tried to figure this out for several days, with no luck.

I am trying to use [ImportMany] to import from a directory, a complete DLL, with an export of type IEditorSystem, which have custom metadata like IEditorSystemMetadata. First, I would like to get metadata and push it onto some text fields, etc., so that the user can choose which Editor system to use, and when selected, load this system ...

I follow the examples as best as possible, that's what I still have.

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

This is what it should import:

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

and launch:

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. Displays both MyEditorSystem and viewmodel with ImportMany, but EditorSystemList is never populated. I get no layout errors.

, - Lazy < > ,

public ObservableCollection<IEditorSystem> EditorSystemList

.

, , , Cinch, MEFedMVVM, MEF. , , .

, , - ?

Update:

IComposer, .

ImportMany , . - , , , .

: IEditorSystem DLL, . dll . - dll. , MEF, :)

+5
3

, , , ,

public ObservableCollection<Lazy<IEditorSystem, IEditorSystemMetadata>> EditorSystemList  

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

:

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; }
}
+1

, .

, .

:

:

int, bool, string .. Int, ImportMany > , 0.

, ExportMetadata .

,

IMyExportMetadata { int a {get;} b {get; } bool c {get;} }

[ (TypeOf (IMyInterface)) [ExportMetadata ( "a", 0)] [ExportMetadata ( "b", "string" )] [ExportMetadata ( "c", true)] myExportedClass: IMyInterface { }

, , , :

[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;
    }
     */
}
+1

, .

, .

:

:

int, bool, stringEtc. If you put two properties int, for example, ImportMany<Lazy<t,m>>it will not work, and it will always return 0.

for each interface property, you must put the attribute ExportMetadatain the exported class.

eg,

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
{
}
0
source

All Articles