C# MEF合成错误,导出不';它不能正常工作

C# MEF合成错误,导出不';它不能正常工作,c#,mef,composition,C#,Mef,Composition,这是我的表单,应显示导入类的结果: public partial class Form1 : Form { [Import(typeof(ITests))] public ITests Template; public string texter; public Form1() { InitializeComponent(); texter = Environment.GetFolderPath(Environment.

这是我的表单,应显示导入类的结果:

public partial class Form1 : Form
{
    [Import(typeof(ITests))]
    public ITests Template;

    public string texter;

    public Form1()
    {
        InitializeComponent();
        texter = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\bin\\dll";
        textBox1.Text = texter;
        string[] array = Directory.GetFiles(texter, "*.dll");

        foreach(string file in array)
        {
            textBox1.Text += Environment.NewLine + file;
        }
        Program();
    }

    public void Program()
    {
        AggregateCatalog catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new DirectoryCatalog(texter));
        Console.WriteLine(catalog.Catalogs);
        try
        {
            CompositionContainer container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
} 
这是我的公共界面: 我已经在两个项目中导入了它们,所以我已经尝试避免assemblyreference错误

namespace ClassLibrary1
{
    public interface ITests
    {
        string Result(string result);
    }
}
这是我的dll和导出代码:

namespace WindowsFormsApplication1
{
    public class Template 
    {
        //Please write all your tests in this class
        //[TestClass]
        public class Tests
        {
            //example of class
            //[TestMethod]
            public class Example : ITests
            {
                [Export(typeof(ITests))]
                public string Result(string res)
                {
                    string resa = res + " dit is door de test gegaan";
                    return resa;
                }
            }

            //[TestMethod]
            public class ExampleTest2
            {
            }
        }
    }
}
我得到这个错误:

类型的第一次机会例外 'System.ComponentModel.Composition.Primitives.ComponentAlepartException' 发生在System.ComponentModel.Composition.dll中 “WindowsFormsApplication1.vshost.exe”(托管(v4.0.30319)):已加载

'C:\Windows\assembly\GAC\MSIL\Microsoft.VisualStudio.DebuggerVisualizers\11.0.0.0\Uuu b03f5f7f11d50a3a\Microsoft.VisualStudio.DebuggerVisualizers.dll' 这篇作文只产生了一个作文错误。根本原因是 下文提供。查看的CompositionException.Errors属性 更详细的信息

1) 导出“WindowsFormsApplication1.Template+Tests+Example.Result(ContractName=“ClassLibrary1.ITests”)”不能分配给类型“ClassLibrary1.ITests”

导致:无法设置导入 'WindowsFormsApplication1.Form1.Template (ContractName=“ClassLibrary1.ITests”)“部分 “WindowsFormsApplication1.Form1”。要素: WindowsFormsApplication1.Form1.Template (ContractName=“ClassLibrary1.ITests”)--> WindowsFormsApplication1.Form1


看起来你把
[Export]
放错地方了。您正在尝试导出
Result
,它是一个字符串,类型为ITests。相反,导出应该在您的类级别:

[Export(typeof(ITests))]
public class Example : ITests
{
    public string Result(string res)
    {
        string resa = res + " dit is door de test gegaan";
        return resa;
    }
}

如何在VS.NET中设置项目?我将这样做:解决方案中的3个项目:1>Windows窗体应用程序项目,2>类库1项目,3>测试项目库。在#1和#3中设置引用项目2时,请确保使用项目引用而不是Dll引用。(即,在“参照”对话框中,按项目选择参照)。似乎正在发生的是,它认为ITests接口有两个版本(我可能错了,但这是我的解释)。@SonerGönül我看到你编辑了很多帖子。你能不能开始编辑代码块,使其格式正确?您似乎缺少/忽略了这些内容。@默认情况下,我不希望编辑太多帖子,但新加入此网站的人会忽略格式部分。我尽了最大努力,但嘿…@SonerGönül,你能编辑真是太好了。如果代码格式也经过编辑,我会很高兴:)