Question about Import/Export [closed]

Apr 10, 2011 at 6:16 PM

I would like to ask if I declared some Export objects by name and type can I import all of them by using only type ? So later I could call:

Container.GetExportedValueOrDefault<IJob>(name);

For exapmle export:

    [Export("tempJob"typeof(IJob))]
    [ExportMetadata("Name""PrintSomething")]
    public class Job : IJob 

 and import:

    [ImportMany(typeof(IJob))]

    IEnumerable<Lazy<IJobIJobName>> Jobs;
Developer
Apr 11, 2011 at 6:46 AM

So if the name is metadata (as ExportMetadata("Name", "PrintSomething")) then yes you can import all of them, however if you give it a specific contract name like you did in your sample "tempJob" then no you cannot. Importing and Exporting is keyed on a contract name and so that contract name needs to be the same if order to import all of them.

Apr 11, 2011 at 9:18 AM

You can put multiple export statements on your class:

 

[Export("tempJob", typeof(IJob))]
[Export(typeof(IJob))]
[ExportMetadata("Name", "PrintSomething")]
public class Job : IJob

 

Apr 11, 2011 at 9:31 AM
Edited Apr 11, 2011 at 9:34 AM

Now I have something like this, and I think it works how I have expected. Thanks.

    [Export("Job1.Job1, Job1",typeof(IJob))]
    [Export(typeof(IJob))]
    [ExportMetadata("Name""Job")]