views:

139

answers:

2

can I hydrate this inside the class's static constructor?

public class Connect:IDTExtensibility2, IDTCommandTarget
  static Connect()
    {
        //hydrate static properties?
    }
    [Import]
    public static Action<ProjectLogicChecks> Display { get; set; }

[Export(typeof(Action<ProjectLogicChecks>))]
    private static void DisplayResults( CheckProcesses _checkResults)
{
    MessageBox.Show(_checkResults.ProjectLogicCheck.AssemblyName + " has problems=" +
                    _checkResults.ProjectLogicCheck.HasProblems);
}
A: 

No, MEF doesn't support static imports.

Daniel Plaisted
-1 I achieved it, look at my answer.
Maslow
+1 pulling a value from a container and stuffing it in a static property is not an import. The whole point of using a container is that it takes care of injecting dependencies automatically. If you're going to do that yourself, you might as well throw out the container altogether. Reusable, unit testable, loosely coupled code should avoid static members. Static members glue everything together in a big ball of mud.
Wim Coenen
@Wim - visual studio calls it an import, there are no references to the code that provides the plugin and the composition works. Just because using a static in some situations is a bad practice doesn't mean it's not MEF. Also, I have TypeMock, static members are unit testable. So what are you trying to say here?
Maslow
@Maslow The Import attribute that you put on the static member is not doing anything. You could remove that attribute and your code would work the same way. That's why we don't consider it an import.
Daniel Plaisted
@Daniel - ok... so what term should I be using? and how is the import supposed to work? My goal was to hydrate a static property with MEF and I achieved it. What technical details/wording am I missing in my question to make that clear?
Maslow
@wim - My question was about populating a property not about 'importing'. So me using an import attribute incorrectly is not relevant to the question. Your +1 to the answer that says is still petty and off-topic.
Maslow
A: 

It was easier than I thought it would be.

 static Connect()
    {
        var batch = new CompositionBatch( );
        CompositionContainer container;
        var reflectionCatalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly( ));

        var extensionPath = System.IO.Path.Combine(Environment.CurrentDirectory, "extensions");
        if (System.IO.Directory.Exists(extensionPath))
        {
            var directoryCatalog = new DirectoryCatalog(extensionPath);
            var defaultCatalogEp = new CatalogExportProvider(reflectionCatalog);
            container=new CompositionContainer(directoryCatalog, defaultCatalogEp);
            defaultCatalogEp.SourceProvider=container;
        }
        else
            container = new CompositionContainer(reflectionCatalog);

        container.Compose(batch);
 //Setting a static property
        Display=container.GetExportedValue<Action<IEnumerable< ProjectLogicChecks>>>( );
    }

Changed the type to Action<IEnumerable<ProjectLogicChecks>> so that I could display results for multiple projects or a whole solution instead of just the one.

I followed this article to get the static property set, then this to provide local defaults in case there is no extension present.

Maslow