Access to a unknown class in a container

Mar 23, 2010 at 7:53 PM

I an assembly is a class which is exported via MEF and another class which is isn't imported and exported. How can I gain access to the class which is not (imported and) exported via MEF in the container? The problem is that the class which is should be exported is used in client applications but the other class isn't. The reason is that the have a dependency between these two classes which I don't want. The class which is not exported should be unknown to the client application. Exports and imports may not be declared.

Here an example:

[Export(typeof(IHello))]
[ExportMetadata("SomeInfos", "ClassLibrary1.Info")]
public class Test : IHello
{
    #region IHello Member

    public string GetHello()
    {
        return "Hello!";
    }

    #endregion
}

public class Info
{
    public Info()
    {
        Console.WriteLine(this.ToString());
    }
}

// ##############################################################

public class TestApp
{
    [Import]
    private Lazy<IHello, Dictionary<string, object>> hello;

    internal void Run()
    {
        var catalog = new DirectoryCatalog("Plugins");
        var container = new CompositionContainer(catalog);

        container.ComposeParts(this);

        string strClass = hello.Metadata["SomeInfos"].ToString();

        // Access to the Info class from here or in the container but without any dependencies (import/export)

        Debugger.Break();

    }
}


I want to access the Info class by using the ExportMetadata Attribute.

Thank you in advance
int

Mar 23, 2010 at 10:40 PM

Create your own class derived from the Info class, and add any attributes you want.

Mar 24, 2010 at 3:12 PM

The info class should not have any dependencies on the library. I want to gain access to the class without deriving or any dependencies. My intention is to invoke the info-class from a client application through a string.

 

Developer
Mar 24, 2010 at 4:21 PM

So I'm assuming that the Test class and the Info class are in different assemblies, and I'm also assuming that the Info class is in some assembly that you either cannot or don't want to take a dependency on MEF so you cannot actually attribute the class to be found by MEF. 

In such a case there isn't anything that MEF can really do automatically for you, ExportMetadata doesn't do anything magical it is only a simple bag of data. Give what you have right now you could simply take the string and use Activator.CreateInstance to get an object instance (you might also need the assembly information if the assembly isn't already loaded). 

If you know the list of these objects your TestApp could register them as Exports in the container by calling container.AddExportedValue<Info>(new Info()), if you don't have the types specifically but you know the assemblies they live you could write your own MEF catalog that would find all the types and automatically Export them without the attributes but that is perhaps more work than you might want to do.

If you can explain what your underlying scenario is perhaps we could come up with another way for you to accomplish your goals.

Mar 24, 2010 at 7:42 PM

An alternative option is for making non-MEF things appear to MEF is to use an adapter part that has a property export. See this post for more on that approach. Depending on your scenario it might require exposing a static member on the adapter in order to set the value within the part itself.

Glenn

Mar 24, 2010 at 7:43 PM

In that post you can jump to the section on "What else can I use this for"