简体   繁体   中英

How to use DLR with MEF in .Net 4.0?

There were promises from the MEF team that MEF will support DLR plugins in the .Net 4.0. Did it happen already and I can [Import] some IronPython objects?

If yes, any links to the topic would be helpful.

The default programming model does not support the DLR, but other programming models can be written that will support it, and that can be used together with the default programming model.

I know this is old, but you can look at http://github.com/JogoShugh/IronPythonMef or look for the NuGet package.

I pulled some code out of a project called ILoveLucene by Bruno Lopes, and turned it into this independent repo and package. It is just getting started, but has some examples included, and unit tests.

Here's an example:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Reflection;
using IronPython.Hosting;
using IronPythonMef;

public interface IMessenger
{
    string GetMessage();
}

public interface IConfig
{
    string Intro { get; }
}

/// <summary>
/// Gets exported from IronPython into the CLR Demo instance.
/// </summary>
public static class PythonScript
{
    public static readonly string Code =
@"
@export(IMessenger)
class PythonMessenger(IMessenger):
    def GetMessage(self):
        return self.config.Intro + ' from IronPython'

    @import_one(IConfig)
    def import_config(self, config):
        self.config = config
";
}

/// <summary>
/// Also gets exported into the Demo instance.
/// </summary>
[Export(typeof(IMessenger))]
public class ClrMessenger : IMessenger
{
    [Import(typeof(IConfig))]
    public IConfig Config { get; set; }

    public string GetMessage()
    {
        return Config.Intro + " from C#!";
    }
}

/// <summary>
/// This will get imported into both the IronPython class and ClrMessenger.
/// </summary>
[Export(typeof(IConfig))]
public class Config : IConfig
{
    public string Intro
    {
        get { return "Hello"; }
    }
}

public class Demo
{
    [ImportMany(typeof(IMessenger))]
    public IEnumerable<IMessenger> Messengers { get; set; }

    public Demo()
    {
        // Create IronPython
        var engine = Python.CreateEngine();
        var script = engine.CreateScriptSourceFromString(PythonScript.Code);

        // Configure the engine with types
        var typesYouWantPythonToHaveAccessTo = new[] { typeof(IMessenger), typeof(IConfig) };
        var typeExtractor = new ExtractTypesFromScript(engine);
        var exports = typeExtractor.GetPartsFromScript(script,
            typesYouWantPythonToHaveAccessTo);

        // Compose with MEF
        var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        var container = new CompositionContainer(catalog);
        var batch = new CompositionBatch(exports, new ComposablePart[] { });
        container.Compose(batch);
        container.SatisfyImportsOnce(this);
    }

    public static void Main(string[] args)
    {
        var demo = new Demo();

        foreach (var messenger in demo.Messengers)
        {
            Console.WriteLine(messenger.GetMessage());
        }

        Console.Read();
    }
}

The output is simply:

Hello from IronPython!
Hello from C#!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM