我似乎无法在常规的Windows DLL中加载从PCL DLL导出的类。
我使用的是Nuget软件包:http://www.nuget.org/packages/Microsoft.Composition/ (Microsoft )1.0.27
传递代码(在常规DLL中):
using System.ComponentModel.Composition;
namespace Normal
{
[Export]
public class TestExport
{
}
}失败代码(在PCL DLL中):
using System.Composition;
namespace PCL
{
[Export]
public class TestExport
{
}
}单元测试(常规单元测试项目):
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace UnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(PCL.TestExport).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Normal.TestExport).Assembly));
var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
CompositionBatch batch = new CompositionBatch();
batch.AddExportedValue(container);
container.ComposeParts(batch);
//Passes
PCL.TestExport constructed = new PCL.TestExport();
Normal.TestExport constructed2 = new Normal.TestExport();
//Passes
Normal.TestExport passes = container.GetExportedValue<Normal.TestExport>();
//Fails
PCL.TestExport e = container.GetExportedValue<PCL.TestExport>();
}
}
}发布于 2014-08-19 22:50:19
您的常规DLL和单元测试项目使用的是System.ComponentModel.Composition,即“mef1”。MEF 1对System.Composition属性一无所知(MEF 2)。
如果您可以在所有项目中使用MEF 2,那么它应该可以工作。
https://stackoverflow.com/questions/25383890
复制相似问题