12

I have a static class in my solution which is used to work with various assemblies. I want to link them through MEF, so I made a field in a class.

[Import(typeof(A))]
    static private A _a1;

Then I have a method to which I pass assembly name as an argument:

    public static A LoadPackage(string filePath)
    {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(filePath));
            var _container = new CompositionContainer(catalog);
            ???
    }

So is there a way now to import type from assembly specified by filepath?

I can't do:

_container.ComposeParts(this);

since class is `static and neither can I do this

_container.ComposeParts(_a1);

(which might be completely wrong to begin with) since A doesn't have any constructors(so _a1 is null)

pb2q
  • 58,613
  • 19
  • 146
  • 147
noaRAVE
  • 432
  • 1
  • 5
  • 19
  • 1
    Is there a reason that it needs to be static? Anything you Import/Export will be a Singelton by default, which is better than having a static class in most cases. – eandersson Jan 09 '12 at 09:29
  • this is one of the options i'm considering. The reason to keep it static is that i don't want to change code all over my project.(i.e - I'm lazy+) ) – noaRAVE Jan 09 '12 at 09:45
  • I am pretty sure that you can't use static classes directly with MEF, but I don't know that for sure. – eandersson Jan 09 '12 at 09:46
  • I am not sure I understand exactly what you are trying to do, but if you load the DLLs into your host application you should be able to import stuff between the DLLs as well. Without having to recompose the parts. – eandersson Jan 09 '12 at 10:08
  • well that's how it currently works(through attributes). But in the other part of application MEF is used to provide similar services and my superior wants to use same solution in both situations. Hmmm, or maybe he wants to toy with MEF :P – noaRAVE Jan 09 '12 at 10:51
  • I think that you can simply add new search paths to your current solution and simply call the `Refresh` method of the `DirectoryCatalog`. – eandersson Jan 09 '12 at 10:57
  • Sorry, i'm kinda lost, in which way does it solve my problem? – noaRAVE Jan 09 '12 at 11:01
  • It might just be me not understanding the problem, or at least not the core problem why you are trying to implement this. But, from my understanding you are trying to load components in a different location, not necessarily at the same time as you load the base components? Either way, you wouldn't be able to use the static methods, or at least not import or export them as static directly. – eandersson Jan 09 '12 at 11:09
  • no, no, nothing that complicated. the problem is that i'm lazy to change my class from static, so i just tried to use MEF with static class. I guess i will just make the class non-static+) – noaRAVE Jan 09 '12 at 11:12
  • Hah, yea I probably read too much into it. I guess a wrapper would work though, but I think it's best to change them to normal classes and have MEF turn them into Singeltons. MEF is essentially similar to the Unity in the way it handles injection. http://akashkava.com/blog/391/mef-vs-unity-in-composite-application-prism/ – eandersson Jan 09 '12 at 11:16

2 Answers2

12

MEF is designed to create and initialize objects for you. It doesn't deal with state in static classes.

I suggest that you make the class and its fields non-static, and mark it with [PartCreationPolicy(CreationPolicy.Shared)] if you want to enforce singleton behavior.

See also this other question on MEF and the singleton pattern.

Community
  • 1
  • 1
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
10

Well it turns out that method I was looking for is GetExportedValue (yeah, I overlooked basic functionality):

static private A _a1;

public static A LoadPackage(string filePath)
{
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(filePath));
        var _container = new CompositionContainer(catalog);
        _a1 = _container.GetExportedValue<A>();
}

And I got my field filled (just in case, I already moved it to another class, and it looks neat and clean now)

pb2q
  • 58,613
  • 19
  • 146
  • 147
noaRAVE
  • 432
  • 1
  • 5
  • 19
  • Not a good solution - as mentioned by Wim above, much better to just use an MEF import of a Shared part and avoid static classes/methods wherever possible. – Stephen Drew Mar 29 '12 at 22:38