1

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

default
  • 11,485
  • 9
  • 66
  • 102
machie27
  • 137
  • 1
  • 8
  • How do you have your projects set up in VS.NET? This is how I would do it: 3 projects in your solution: 1> Windows form app project, 2> ClassLibrary1 project, 3> Tests project library. When setting up references in #1 and #3 to reference project 2, make sure you use the project reference and not the Dll reference. (That is, in the references dialog, select the reference by project). What appears to be happening is that it thinks there are two version of the ITests interface (I could be wrong, but that's my interpretation). – Pete Feb 28 '13 at 16:24
  • @SonerGönül I see that you edit a lot of posts. Could you **please** start editing the code blocks as well to be well formed? You seem to be missing/ignoring those. – default Feb 28 '13 at 17:07
  • @Default I wish _not to_ edit a lot of posts, but people are who joined new to this site ignore the formatting part. I try to my best but hey.. – Soner Gönül Feb 28 '13 at 17:10
  • @SonerGönül and it's great that you do edit. I'd just be happy if the code formatting was edited as well :) – default Feb 28 '13 at 17:16

1 Answers1

3

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;
    }
}
ChrisO
  • 5,068
  • 1
  • 34
  • 47