これは、インポートしたクラスの結果を表示するフォームです。
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.ComposablePartException' の最初の例外が System.ComponentModel.Composition.dll 'WindowsFormsApplication1.vshost.exe' で発生しました (Managed (v4.0.30319)): ロード済み
'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.DebuggerVisualizers\11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.DebuggerVisualizers.dll' 構成で 1 つの構成エラーが発生しました。根本的な原因を以下に示します。詳細については、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