Interface Inheritence

Nov 27, 2008 at 7:06 AM
Edited Nov 27, 2008 at 7:08 AM
Hey gang,

If I define two interfaces:

    public interface IBase { ... }
    public interface IDerived : IBase { ... }

... then have a class like this:

    [Export(typeof(IDerived))]
    public class BaseAndDerived : IDerived { ... }

Then MEF doesn't seem to find it when I import IBase implementations like this:

    [Import(typeof(IBase))]
    // or even just [Import]
    public IList<IBase> BaseImplementations { get; set; }

Is that by design? Right now I'm having to import a second property (IList<IDerived>) and join them together to get all the IBase implementations.

Cheers,
Matt
Nov 27, 2008 at 8:09 AM

Yes, this is by design. You would need to export IBase explicitly. You can put multiple exports though on BaseAndDerived thus allowing you to import all base implementations.

[Export(typeof(IBase))]

[Export(typeof(IDerived))]

public class BaseAndDerived : IDerived { … }

Does this work?

Glenn

From: mabster [mailto:notifications@codeplex.com]
Sent: Wednesday, November 26, 2008 11:06 PM
To: Glenn Block
Subject: Interface Inheritence [MEF:40922]

From: mabster

Hey gang,

If I define two interfaces:

public interface IBase { ... }
public interface IDerived : IBase { ... }

... then have a class like this:

[Export(typeof(IDervied))]
public class BaseAndDerived : IDerived { ... }

Then MEF doesn't seem to find it when I import IBase implementations like this:

[Import(typeof(IBase))]
public IList<IBase> BaseImplementations { get; set; }

Is that by design? Right now I'm having to import a second property (IList<IDerived>) and join them together to get all the IBase implementations.

Cheers,
Matt

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

Nov 27, 2008 at 8:13 AM
Yep, exporting as both interface types works, but for the sake of making life easier for potential extension authors I'll stick with the idea of importing both types and combining in the host for now.
I wonder ... is there scope to add some sort of named parameter to the ImportAttribute to make this easier? Tell the import to look at base types in the catalogs? Just a thought.
Thanks Glenn,
Matt

From: [email removed]
Sent: Thursday, November 27, 2008 7:09 PM
To: [email removed]
Subject: Re: Interface Inheritence [MEF:40922]

From: gblock

Yes, this is by design. You would need to export IBase explicitly. You can put multiple exports though on BaseAndDerived thus allowing you to import all base implementations.

[Export(typeof(IBase))]

[Export(typeof(IDerived))]

public class BaseAndDerived : IDerived { … }

Does this work?

Glenn

Nov 27, 2008 at 9:22 AM
Ok, so if I'm reading the source right, when I type this:

[Import(typeof(IFoo))]

That's actually translating "typeof(IFoo)" into a string that MEF can use to locate exports, right? So it's kind of similar to just typing:

[Import(typeof(IFoo).ToString())]

... except that I'm not sure that it literally does a "ToString" on the type.

If that's right, then making the ImportAttribute search for a type and all its ancestor types would be non-trivial.
Nov 27, 2008 at 9:27 AM
I guess as an optional behavior that could be useful. But I don't see it as very high prio. Better to be explicit and say what you want.

But this might be important to you...

M.

On Thu, Nov 27, 2008 at 10:22, mabster <notifications@codeplex.com> wrote:

From: mabster

Ok, so if I'm reading the source right, when I type this:

[Import(typeof(IFoo))]

That's actually translating "typeof(IFoo)" into a string that MEF can use to locate exports, right? So it's kind of similar to just typing:

[Import(typeof(IFoo).ToString())]

... except that I'm not sure that it literally does a "ToString" on the type.

If that's right, then making the ImportAttribute search for a type and all its ancestor types would be non-trivial.

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




--
Magnus Mårtensson
Senior Consultant - Scrum Master - MCSD, MCTS
Dotway AB

Tel: +46 (768) 51 00 36

http://blog.noop.se/
Nov 27, 2008 at 9:30 AM
Meh. I can work around it. I'm just thinking that it might be a little weird for newcomers (like me) who have a single:

[Import]
public IList<IFoo> { get; set; }

... in their app, and don't get any objects imported because all the exports are actually implementing a descendant of IFoo.

It's probably an edge case but perhaps something to think about for vNext.
Nov 27, 2008 at 9:32 AM

Yes this is exactly correct. Contracts are just strings behind the scenes, we get the qualified name if you pass a type. I was going to send that but wasn’t sure if that was what you needed or not.

Glenn

From: mabster [mailto:notifications@codeplex.com]
Sent: Thursday, November 27, 2008 1:23 AM
To: Glenn Block
Subject: Re: Interface Inheritence [MEF:40922]

From: mabster

Ok, so if I'm reading the source right, when I type this:

[Import(typeof(IFoo))]

That's actually translating "typeof(IFoo)" into a string that MEF can use to locate exports, right? So it's kind of similar to just typing:

[Import(typeof(IFoo).ToString())]

... except that I'm not sure that it literally does a "ToString" on the type.

If that's right, then making the ImportAttribute search for a type and all its ancestor types would be non-trivial.

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