Importing collections

Mar 30, 2009 at 11:20 PM
I ran into an issue today that I wanted to share. I have a custom collection (FooCollection) that implements a custom interface (IFooCollection) which inherits from IEnumerable<IFoo>. I have marked the FooCollection class with [Export(typeof(IFooCollection))] and now want to import the singleton instance of that implementation in the places I need it. The obvious:

    [Import(typeof(IFooCollection))]
    private IFooCollection _xxx;

does not work, because when MEF sees the type of the field is derived from IEnumerable, it assumes I want a new collection of IFoo's rather than an instance of FooCollection. I can work around this by doing this:

    [Import(typeof(IFooCollection))]
    private object _xxx;
    private IFooCollection _XXX { get { return _xxx as IFooCollection; } }

It would seem more natural if there were some sort of parameter for the Import() attribute that would let me tell MEF to give me the thing that implements IFooCollection rather than create a new collection of IFoo's - something like [Import(typeof(IFooCollection), IgnoreEnumerable=true)].

Is there already a way to do this?

Ron
Mar 30, 2009 at 11:24 PM

Hi Ron,

The next drop of MEF should contain a fix for this – briefly, the behaviour you require will become the default. Importing more than one item will be achieved using the [ImportMany] attribute.

Cheers,

Nick

From: vandermeulen [mailto:notifications@codeplex.com]
Sent: Monday, March 30, 2009 4:21 PM
To: Nicholas Blumhardt
Subject: Importing collections [MEF:51771]

From: vandermeulen

I ran into an issue today that I wanted to share. I have a custom collection (FooCollection) that implements a custom interface (IFooCollection) which inherits from IEnumerable<IFoo>. I have marked the FooCollection class with [Export(typeof(IFooCollection))] and now want to import the singleton instance of that implementation in the places I need it. The obvious:

[Import(typeof(IFooCollection))]
private IFooCollection _xxx;

does not work, because when MEF sees the type of the field is derived from IEnumerable, it assumes I want a new collection of IFoo's rather than an instance of FooCollection. I can work around this by doing this:

[Import(typeof(IFooCollection))]
private object _xxx;
private IFooCollection _XXX { get { return _xxx as IFooCollection; } }

It would seem more natural if there were some sort of parameter for the Import() attribute that would let me tell MEF to give me the thing that implements IFooCollection rather than create a new collection of IFoo's - something like [Import(typeof(IFooCollection), IgnoreEnumerable=true)].

Is there already a way to do this?

Ron

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Mar 30, 2009 at 11:29 PM

Sorry – a slight correction. The new drop will include [ImportMany] but the default behaviour of [Import] will be unchanged, to give everyone time to migrate. More details when the code is available.

(Thanks Glenn for the correction.)

Nick

From: nblumhardt [mailto:notifications@codeplex.com]
Sent: Monday, March 30, 2009 4:25 PM
To: Nicholas Blumhardt
Subject: Re: Importing collections [MEF:51771]

From: nblumhardt

Hi Ron,

The next drop of MEF should contain a fix for this – briefly, the behaviour you require will become the default. Importing more than one item will be achieved using the [ImportMany] attribute.

Cheers,

Nick

From: vandermeulen [mailto:notifications@codeplex.com]
Sent: Monday, March 30, 2009 4:21 PM
To: Nicholas Blumhardt
Subject: Importing collections [MEF:51771]

From: vandermeulen

I ran into an issue today that I wanted to share. I have a custom collection (FooCollection) that implements a custom interface (IFooCollection) which inherits from IEnumerable<IFoo>. I have marked the FooCollection class with [Export(typeof(IFooCollection))] and now want to import the singleton instance of that implementation in the places I need it. The obvious:

[Import(typeof(IFooCollection))]
private IFooCollection _xxx;

does not work, because when MEF sees the type of the field is derived from IEnumerable, it assumes I want a new collection of IFoo's rather than an instance of FooCollection. I can work around this by doing this:

[Import(typeof(IFooCollection))]
private object _xxx;
private IFooCollection _XXX { get { return _xxx as IFooCollection; } }

It would seem more natural if there were some sort of parameter for the Import() attribute that would let me tell MEF to give me the thing that implements IFooCollection rather than create a new collection of IFoo's - something like [Import(typeof(IFooCollection), IgnoreEnumerable=true)].

Is there already a way to do this?

Ron

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Mar 30, 2009 at 11:29 PM

Wonderful! Thanks for the quick reply.

Ron

From: nblumhardt [mailto:notifications@codeplex.com]
Sent: Monday, March 30, 2009 4:25 PM
To: VANDERMEULEN,RON (A-LakeStevens,ex1)
Subject: Re: Importing collections [MEF:51771]

From: nblumhardt

Hi Ron,

The next drop of MEF should contain a fix for this – briefly, the behaviour you require will become the default. Importing more than one item will be achieved using the [ImportMany] attribute.

Cheers,

Nick

From: vandermeulen [mailto:notifications@codeplex.com]
Sent: Monday, March 30, 2009 4:21 PM
To: Nicholas Blumhardt
Subject: Importing collections [MEF:51771]

From: vandermeulen

I ran into an issue today that I wanted to share. I have a custom collection (FooCollection) that implements a custom interface (IFooCollection) which inherits from IEnumerable<IFoo>. I have marked the FooCollection class with [Export(typeof(IFooCollection))] and now want to import the singleton instance of that implementation in the places I need it. The obvious:

[Import(typeof(IFooCollection))]
private IFooCollection _xxx;

does not work, because when MEF sees the type of the field is derived from IEnumerable, it assumes I want a new collection of IFoo's rather than an instance of FooCollection. I can work around this by doing this:

[Import(typeof(IFooCollection))]
private object _xxx;
private IFooCollection _XXX { get { return _xxx as IFooCollection; } }

It would seem more natural if there were some sort of parameter for the Import() attribute that would let me tell MEF to give me the thing that implements IFooCollection rather than create a new collection of IFoo's - something like [Import(typeof(IFooCollection), IgnoreEnumerable=true)].

Is there already a way to do this?

Ron

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Mar 31, 2009 at 3:39 PM
This sounds great. Thanks for the quick reply.

Ron