كيفية استخدام DLR مع MEF في .Net 4.0?
-
21-08-2019 - |
سؤال
كانت هناك وعود من MEF فريق MEF دعم DLR الإضافات في .Net 4.0.لم يحدث بالفعل وأنا يمكن أن [استيراد] بعض IronPython الكائنات ؟
إذا كانت اإلجابة نعم ، أي روابط لهذا الموضوع المفيد.
المحلول
الافتراضي نموذج البرمجة لا يدعم DLR ، ولكن البرمجة الأخرى نماذج يمكن أن تكون مكتوبة من شأنها أن تدعم ذلك ، و التي يمكن استخدامها جنبا إلى جنب مع الافتراضي نموذج البرمجة.
- بلوق وظيفة على MEF الأوليات (التي تستخدم لخلق نماذج البرمجة).
- بلوق وظيفة مع بعض الأفكار حول كيفية روبي نموذج البرمجة يمكن أن تعمل
- كيفية كتابة مخصص الأمر Intellipad (Intellipad يستخدم MEF و البرمجة بايثون نموذج)
نصائح أخرى
أعرف أن هذا هو القديم ، ولكن يمكنك أن تبحث في http://github.com/JogoShugh/IronPythonMef أو ابحث عن حزمة NuGet.
سحبت بعض رمز من مشروع يسمى ILoveLucene من قبل برونو Lopes, وحولتها إلى هذا المستقلة الريبو و الحزمة.أنها بدأت للتو, ولكن لديه بعض الأمثلة ، وحدة الاختبارات.
هنا مثال:
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();
}
}
الإخراج هو ببساطة:
Hello from IronPython!
Hello from C#!
لا تنتمي إلى StackOverflow