3

I have 2 MEF components. Let it be component A and component B.

What I need is to be able to access a class from component B in component A without referencing to it. Then I would like to instantiate object of the class manually.

Currently I see MEF allows instantiating an object automatically using [Import]. It uses interface which requires to be referenced to.

Can I use data types from another assemblies without referencing to it? Is there such mechanism supported by MEF?

Oleh
  • 33
  • 3
  • Can you update your question with an example of the type you wish to be accessible, as well as some pseudo code of how you *think* you want to use it? – Matthew Abbott Jun 09 '11 at 11:27

3 Answers3

1

You can instantiate a class via reflection without having a hard reference to the file. You don't need MEF for that.

Community
  • 1
  • 1
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
  • MEF just makes it easier, that's all. – Josh G Jun 10 '11 at 20:00
  • True. With reflection, you don't even need a hard coded contract either (you can just call methods by name). It seems like that's what the OP wants. – Scott Whitlock Jun 10 '11 at 20:02
  • Yes, and that can be useful, but it can be very messy. You'll still have an implicit interface consisting of expected function names and parameters. Why not define a real interface so that everyone knows what it is and the compiler can enforce it? – Josh G Jun 10 '11 at 20:05
  • @Josh G - absolutely. If you can do it, use a commonly agreed-upon interface. However, I have seen situations where "Duck Typing" would be the preferred approach. – Scott Whitlock Jun 10 '11 at 20:11
  • It required MEF implementation. – Oleh Oct 30 '12 at 12:08
0

Then I would like to instantiate object of the class manually.

Maybe you would be better of to do it manually by loading the assembly and pick the desired type from it instead of using MEF.

ba__friend
  • 5,783
  • 2
  • 27
  • 20
0

There are a couple of ways to do this.

First, you need to define a common interface that both assemblies understand. This could be a "PublicInterfaces" library that both of these assemblies reference, or it could be inside of assembly A (B references A, but not the other way around).

In B, export the type using this interface.

B has to be in the container's catalog. Either reference assembly B explicitly in an AssemblyCatalog, or create a DirectoryCatalog and point it at the directory that will contain assembly B.

In A, instead of using Import attributes, in code call GetExportedValue<T>() on the container. The code looks something like this:

// Known by A and B
public interface CommonInterface 
{
   // ...
}

// In B, not A
[Export(typeof(CommonInterface))]
public class BClass : CommonInterface
{
   // ...
}

// In A where you want to manually create class B
CommonInterface objB = _container.GetExportedValue<CommonInterface>();
Josh G
  • 14,068
  • 7
  • 62
  • 74
  • The only way you could use the AssemblyCatalog without A referencing B would be to look the assembly up with reflection. – Josh G Jun 10 '11 at 19:59
  • Figured that out. Created a separate assembly C which contains interfaces, constants and common types used along assemblies A and B. Both A and B should be referenced to C. In that case "export and import contract sides" know everything they should know (interfaces, common types, constants). So every MEF component should have strong reference to at least one assembly which defines at least its interface. No any other strong references required. MEF will take care about that. I have accepted this answer. Thank you! – Oleh Oct 30 '12 at 12:10