这是我的表单,它应该显示我导入的类的结果:
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);
}
}
}
这是我的公共界面:我在两个项目中都导入了它们,所以我已经尝试避免组装引用错误
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.dll 'WindowsFormsApplication1.vshost.exe'(托管(v4.0.30319))中发生了“System.ComponentModel.Composition.Primitives.ComposablePartException”类型的第一次机会异常:已加载
'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.DebuggerVisualizers\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.DebuggerVisualizers.dll' 合成产生了单一合成错误。下面提供了根本原因。查看 CompositionException.Errors 属性以获取更多详细信息。
1) 导出 'WindowsFormsApplication1.Template+Tests+Example.Result (ContractName="ClassLibrary1.ITests")' 不可分配给类型 'ClassLibrary1.ITests'。
导致:无法在“WindowsFormsApplication1.Form1”部分设置导入“WindowsFormsApplication1.Form1.Template (ContractName="ClassLibrary1.ITests")”。元素:WindowsFormsApplication1.Form1.Template (ContractName="ClassLibrary1.ITests") --> WindowsFormsApplication1.Form1