C#-MEF-从可移植类库导入
我似乎无法在常规Windows DLL中加载从PCL DLL导出的类 我正在使用Nuget软件包:(Microsoft Composition(MEF 2)1.0.27) 传递代码(在常规DLL中): 失败代码(在PCL DLL中): 单元测试(常规单元测试项目):C#-MEF-从可移植类库导入,c#,mef,portable-class-library,C#,Mef,Portable Class Library,我似乎无法在常规Windows DLL中加载从PCL DLL导出的类 我正在使用Nuget软件包:(Microsoft Composition(MEF 2)1.0.27) 传递代码(在常规DLL中): 失败代码(在PCL DLL中): 单元测试(常规单元测试项目): 使用系统; 使用Microsoft.VisualStudio.TestTools.UnitTesting; 使用PCL; 使用System.ComponentModel.Composition; 使用System.Component
使用系统;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用PCL;
使用System.ComponentModel.Composition;
使用System.ComponentModel.Composition.Hosting;
命名空间单元测试
{
[测试类]
公共类UnitTest1
{
[测试方法]
公共void TestMethod1()
{
AggregateCatalog catalog=新的AggregateCatalog();
catalog.Catalogs.Add(新的AssemblyCatalog(typeof(PCL.TestExport.Assembly));
catalog.Catalogs.Add(新的AssemblyCatalog(typeof(Normal.TestExport.Assembly));
var container=新的CompositionContainer(目录,CompositionOptions.DisablesInRejection);
CompositionBatch=新的CompositionBatch();
batch.AddExportedValue(容器);
容器、组件(批次);
//通行证
PCL.TestExport constructed=新的PCL.TestExport();
Normal.TestExport constructed2=新建Normal.TestExport();
//通行证
Normal.TestExport passs=container.GetExportedValue();
//失败
PCL.TestExport e=container.GetExportedValue();
}
}
}
您的常规DLL和单元测试项目正在使用System.ComponentModel.Composition,即“MEF 1”。MEF1对System.Composition属性(MEF2)一无所知
如果您可以在所有项目中使用MEF2,那么它应该可以工作
using System.ComponentModel.Composition;
namespace Normal
{
[Export]
public class TestExport
{
}
}
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>();
}
}
}