这是我的撰写部分,我在asmCatalog中看到了应该加载的目录编号正确的编号。
public void Compose()
{
var aggrCatalog = new AggregateCatalog();
//A directory catalog
var dirCatalog = new DirectoryCatalog(Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) + "\\Extensions", "*.dll");
//An assembly catalog
var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
aggrCatalog.Catalogs.Add(dirCatalog);
aggrCatalog.Catalogs.Add(asmCatalog);
//Create a container THE CONTAINER DOES CONTAIN 5 PARTS AND
// LOOK PERFECTLY OK. The 5 parts consist of 3 from this assembly
// and two from the external assembly
var container = new CompositionContainer(aggrCatalog);
// IT DOES NOT COMPOISE THE 2 PARTS THAT WERE LOADED FROM THE EXTERNAM ASSY
container.ComposeParts(this);
}
我正在使用此界面和属性:
namespace Q95Interface
{
public interface IQ95_EventHandler
{
void HandleQ95Event();
void HandleQ95Event(string s);
}
public interface IUIExtensionDetails
{
string Event_Group_Name { get; }
string Event_Name { get; }
}
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class Q95_EventHandlerAttribute : ExportAttribute
{
public Q95_EventHandlerAttribute() : base(typeof(IQ95_EventHandler)) { }
public string Event_Group_Name { get; set; }
public string Event_Name { get; set; }
}
}
我要导入的实现是(进入本地程序集的实现看起来像IDENTICAL并加载并完美地工作)
namespace MefTest
{
[Export(typeof(IQ95_EventHandler))]
[ExportMetadata("EventType", "START_ORDER_EXTERNAL")]
public class Q95_Event_Handler4 : IQ95_EventHandler
{
public void HandleQ95Event()
{
Console.WriteLine("HANDLE EVENT START_ORDER");
}
public void HandleQ95Event(string s) { Console.WriteLine(s); }
}
[Export(typeof(IQ95_EventHandler))]
[ExportMetadata("EventType", "STOP_ORDER_EXTERNAL")]
public class Q95_Event_Handler5 : IQ95_EventHandler
{
public void HandleQ95Event()
{
Console.WriteLine("\nHANDLE EVENT STOP_ORDER\n");
}
public void HandleQ95Event(string s) { Console.WriteLine(s); }
}
}
我已经分别编译成了一个dll接口和属性定义
答案 0 :(得分:1)
您的[Import]
定义为什么?我怀疑这可能是因为您的导出不导出元数据。实际上,您已经创建了一个自定义的导出属性,并且您没有使用它。
尝试更改出口:
[Q95_EventHandler(Event_Group_Name = "Something", Event_Name = "Something Else")]
public class Q95_Event_Handler5 : IQ95_EventHandler
{
}
确保您相应导入:
[ImportMany]
public IEnumerable<Lazy<IQ95_EventHandler, IUIExtensionDetails>> Handlers { get; set; }
值得一看Daniel Plaisted的博客文章How to Debug and Diagnose MEF Failures,以了解您可能遇到的问题。