Mono 上的 MEF 无法正常工作?

发布于 2024-07-12 12:05:52 字数 3846 浏览 5 评论 0原文

我制作了一个非常简单的 MEF 示例,它在 .NET 上运行, 但在 Mono 上不能正常工作。

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;

namespace Vialis
{
    class Program
    {
        [Import(typeof(ILedController))]
        public List<ILedController> Controllers
        {
            get;
            set;
        }

        static void Main(string[] args)
        {
            new Program();
        }

        public Program()
        {
            compose();
            selectController();

            Console.ReadLine();
        }

        private void compose()
        {
            var catalog = new DirectoryPartCatalog("controllers");
            var container = new CompositionContainer(catalog);

            container.AddPart(this);
            container.Compose();
        }

        private void selectController()
        {
            Console.Clear();
            Console.WriteLine("Please select the controller to use\n");

            byte i = 0;

            foreach (var controller in Controllers)
            {
                Console.WriteLine("\t{0}) {1}", i, controller.ToString());
                i++;
            }

            Console.Write("\nYour selection: ");
            var input = Convert.ToInt32(Console.ReadLine());

            Controllers[input].DisplayText(10, 10, "Hello World");
        }
    }
}

这是接口:

using System;
using System.Collections.Generic;
using System.Text;

namespace Vialis
{
    public interface ILedController
    {
        void DisplayText(int x, int y, string text);
    }
}

这是第一个实现:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;

namespace Vialis
{
    [Export(typeof(ILedController))]
    public class LyanController : ILedController
    {
        public void DisplayText(int x, int y, string text)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(text);
        }
    }
}

第二个实现:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;

namespace Vialis
{
    [Export(typeof(ILedController))]
    public class VialisController : ILedController
    {
        public void DisplayText(int x, int y, string text)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(text);
        }

    }
}

这是 .NET (Windows) 上发生的情况:

.NET http://lh5.ggpht.com/_GWubgra2SwM/SXl-yoSRLtI/AAAAAAAADwI/sGR0FjLfg8Q/controller-selection-windows.jpg

选择控制器:

.NET 1 http: //lh3.ggpht.com/_GWubgra2SwM/SXl-yYzs-aI/AAAAAAAADwE/WomfaQqv_Xc/vialis-controller-windows.jpg

.NET 2 http://lh6.ggpht.com/_GWubgra2SwM/SXl-yE1Q-cI/AAAAAAADwA/qznnEkiNokA/lyan- controller-windows.jpg

但是使用 Mono 2.2 程序集不会加载:

单声道 http://lh5.ggpht.com/_GWubgra2SwM/SXl-xw0YXOI/AAAAAAAADv8/7j2UxJott04/controller-selection-macos.jpg

有什么建议吗?

信息:Google 似乎遇到了一些 picasa 网络问题, 这就是图像无法加载的原因。

图片显示,在 Mac OS 上,没有列出任何控制器。

I've made a very simple MEF sample which runs on .NET,
but doesn't work properly on Mono.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;

namespace Vialis
{
    class Program
    {
        [Import(typeof(ILedController))]
        public List<ILedController> Controllers
        {
            get;
            set;
        }

        static void Main(string[] args)
        {
            new Program();
        }

        public Program()
        {
            compose();
            selectController();

            Console.ReadLine();
        }

        private void compose()
        {
            var catalog = new DirectoryPartCatalog("controllers");
            var container = new CompositionContainer(catalog);

            container.AddPart(this);
            container.Compose();
        }

        private void selectController()
        {
            Console.Clear();
            Console.WriteLine("Please select the controller to use\n");

            byte i = 0;

            foreach (var controller in Controllers)
            {
                Console.WriteLine("\t{0}) {1}", i, controller.ToString());
                i++;
            }

            Console.Write("\nYour selection: ");
            var input = Convert.ToInt32(Console.ReadLine());

            Controllers[input].DisplayText(10, 10, "Hello World");
        }
    }
}

This is the interface:

using System;
using System.Collections.Generic;
using System.Text;

namespace Vialis
{
    public interface ILedController
    {
        void DisplayText(int x, int y, string text);
    }
}

This is the first implementation:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;

namespace Vialis
{
    [Export(typeof(ILedController))]
    public class LyanController : ILedController
    {
        public void DisplayText(int x, int y, string text)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(text);
        }
    }
}

The second implementation:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;

namespace Vialis
{
    [Export(typeof(ILedController))]
    public class VialisController : ILedController
    {
        public void DisplayText(int x, int y, string text)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(text);
        }

    }
}

This is what happens on .NET (Windows):

.NET http://lh5.ggpht.com/_GWubgra2SwM/SXl-yoSRLtI/AAAAAAAADwI/sGR0FjLfg8Q/controller-selection-windows.jpg

Selecting the controllers:

.NET 1 http://lh3.ggpht.com/_GWubgra2SwM/SXl-yYzs-aI/AAAAAAAADwE/WomfaQqv_Xc/vialis-controller-windows.jpg

.NET 2 http://lh6.ggpht.com/_GWubgra2SwM/SXl-yE1Q-cI/AAAAAAAADwA/qznnEkiNokA/lyan-controller-windows.jpg

But using Mono 2.2 the assemblies don't load:

Mono http://lh5.ggpht.com/_GWubgra2SwM/SXl-xw0YXOI/AAAAAAAADv8/7j2UxJott04/controller-selection-macos.jpg

Any suggestions ?

INFO: Google seems to be having some picasa web problems,
that is why the images don't load.

The pictures show you that on Mac OS, no controllers are listed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一张白纸 2024-07-19 12:05:52

使用最新的 MEF 版本(预览版 4 - http://www.codeplex.com/MEF)效果很好!

由于该错误不再相关,我投票结束这个问题。

With the latest MEF release (Preview 4 - http://www.codeplex.com/MEF) it works just fine!

Since the bug is no longer relevant I voted to close this question.

离线来电— 2024-07-19 12:05:52

因此,我假设导出是在外部程序集中定义的,因为您使用 DirectoryPartCatalog.. 要么目录中的文件路径处理存在问题,要么问题出在 AttributedAssemblyPartCatalog / AttributedTypesPartCatalog

内部,DirectoryPartCatalog 包装每个发现的程序集到 AttributedAssemblyPartCatalog 中,而 AttributedAssemblyPartCatalog 又使用 AttributedTypesPartCatalog

您最好的选择是将 MEF 项目添加到您的解决方案中,而不是 dll,并在 DirectoryPartCatalog & 的贪婪构造函数中设置断点。 AttributedAssemblyPartCatalog 并逐步执行,直到找到问题

为止不幸的是,我没有单声道机器设置,因此我无法提供更多帮助

So I'm assuming that the exports are defined in external assemblies, because you use the DirectoryPartCatalog.. either there is an issue with the file path handling in the catalog or the problem is in the AttributedAssemblyPartCatalog / AttributedTypesPartCatalog

Internally the DirectoryPartCatalog wraps each discovered assembly into an AttributedAssemblyPartCatalog which in turn uses a AttributedTypesPartCatalog

Your best bet is to add the MEF project into your solution, instead of the dll, and set a break point in the greediest constructors of DirectoryPartCatalog & AttributedAssemblyPartCatalog and step until u find the problem

Unfortunatly I do not have a mono machine setup so I can't help more than that

农村范ル 2024-07-19 12:05:52

将接口和两个实现添加到应用程序程序集中是可行的。
所以我会像你建议的那样进行调试,但需要找到一个合适的单声道调试器。

Adding the interface and the two implementations to the application assembly works.
So I'll have debug like you suggested, need to find a decent debugger for mono though.

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