无法从 EXTERNAL DLL 加载接口的实现
这是我的 Compose 部分,我在 dir Catalog 中看到了应该加载的正确编号,以及在 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; }
}
}
我想要导入的实现是(本地程序集中的实现看起来相同并且加载和工作完美)
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 中
This is my Compose part where I see the in dir Catalog THE RIGHT NUMBER that should be loaded , as well in 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);
}
I am using this interface and attributes:
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; }
}
}
And the implementations that I want to import are ( the ones into the local assembly look IDENTICAL and load and work perfectly )
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); }
}
}
I have separately compiled into a dll the interface and the attribute definitions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
[Import]
定义是什么?我怀疑这可能是因为您的导出不导出元数据。事实上,您已经创建了自定义导出属性,但并未使用它。尝试更改您的导出:
并确保相应地导入:
值得一看 Daniel Plaisted 的博客文章,标题为 如何调试和诊断 MEF 故障以了解您可能会遇到的问题遇到问题。
What is your
[Import]
defined as? I suspect it is likely because your exports don't export metadata. In fact, you've created a customised export attribute, and you're not using it.Try changing your exports:
And ensure you import accordingly:
It's worth looking at Daniel Plaisted's blog article entitled How to Debug and Diagnose MEF Failures to get an understanding of where you might be running into problems.