简体   繁体   中英

MEF Composition Error, Export doesn't work as it should

this is my form which should display the result from my imported class:

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);
        }
    }
} 

this is my public interface: I've imported them in both projects, so I already tried to avoid the assemblyreference error

namespace ClassLibrary1
{
    public interface ITests
    {
        string Result(string result);
    }
}

here is my dll with the export code:

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
            {
            }
        }
    }
}

I get this error:

A first chance exception of type 'System.ComponentModel.Composition.Primitives.ComposablePartException' occurred in System.ComponentModel.Composition.dll 'WindowsFormsApplication1.vshost.exe' (Managed (v4.0.30319)): Loaded

'C:\\Windows\\assembly\\GAC_MSIL\\Microsoft.VisualStudio.DebuggerVisualizers\\11.0.0.0__b03f5f7f11d50a3a\\Microsoft.VisualStudio.DebuggerVisualizers.dll' The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) The export 'WindowsFormsApplication1.Template+Tests+Example.Result (ContractName="ClassLibrary1.ITests")' is not assignable to type 'ClassLibrary1.ITests'.

Resulting in: Cannot set import 'WindowsFormsApplication1.Form1.Template (ContractName="ClassLibrary1.ITests")' on part 'WindowsFormsApplication1.Form1'. Element: WindowsFormsApplication1.Form1.Template (ContractName="ClassLibrary1.ITests") --> WindowsFormsApplication1.Form1

It just looks like you placed your [Export] in the wrong place. You are trying to export Result which is a string as type ITests. Instead, the export should be at your class level:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM