Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

MEF Basic

Knight1988 edited this page Jan 16, 2016 · 2 revisions

Using SharpUtility.MEF

Useage: create InterfaceProject, define baseclass.

public abstract class Export : ExporterBase
{
    public virtual string InHere()
    {
        return string.Empty;
    }
}

create PluginProject, create plugin class inherit from Export class above.

public class Import : Export
{
    public override string InHere()
    {
        return "Lib1";
    }
}

in main project, Create Runner class inherit from RunnerBase.

public class Runner : RunnerBase<Export>
{
    public string DoSomething()
    {
        // Here you can access Exports which contain Dictionary of plugin class
        // Tell our MEF parts to do something.
        return string.Join(" ", Exports.Select(p => p.Value.InHere()));
    }
}

in main program, Create runner by calling

var basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
var cachePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "ShadowCopyCache");
var pluginPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Plugins");
var runner = RunnerManager.CreateRunner<Runner, Export>(domainName, pluginPath, cachePath, basePath);

// Then you can call the plugin functions
var actual = runner.DoSomething();

SharpUtility

Clone this wiki locally