无法从 EXTERNAL DLL 加载接口的实现

发布于 2024-12-03 08:09:30 字数 2384 浏览 0 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2024-12-10 08:09:30

您的[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 的博客文章,标题为 如何调试和诊断 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:

[Q95_EventHandler(Event_Group_Name = "Something", Event_Name = "Something Else")]
public class Q95_Event_Handler5 : IQ95_EventHandler
{

}

And ensure you import accordingly:

[ImportMany]
public IEnumerable<Lazy<IQ95_EventHandler, IUIExtensionDetails>> Handlers { get; set; }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文