MEF .Net Core 1.0 NetStandard 1.6使用参数初始化实例

时间:2017-01-04 17:53:32

标签: c# asp.net-core mef

我正在尝试获取TestPrintTemplate的实例。

我得到一个例外'没有为合同找到导出'字符串" SomeData"'当我调用container.GetExport(contractName)。

如何初始化TestPrintTemplate()构造函数中的参数?

namespace TestPrintTemplate
{

    [Export("TestPrintTemplate",typeof(IPrintingTemplate))]
    public class TestPrintTemplate : IPrintingTemplate
    {

        [ImportingConstructor]
        public TestPrintTemplate([Import("SomeData")]  string data)
        {

        }
        public void Print()
        {
            return;
        }
    }

    public interface IPrintingTemplate
    {
        void Print();
    }


        public static T GetExportedValue<T>(this CompositionHost container, string contractName)
        {

            var result = new List<T>();
            string exportedTypeName = typeof(T).FullName;
            var item = container.GetExport<T>(contractName); // I get an exception here
            result.Add((T)item);
            return result.FirstOrDefault();
        }

project.json:

{
  "version": "1.0.0-*",

  "dependencies": {
    "BinaryFormatter": "1.0.2",
    "NETStandard.Library": "1.6.0",
    "System.Data.SqlClient": "4.1.0",
    "System.Reflection.Emit": "4.0.1",
    "System.Threading.Thread": "4.0.0",
    "System.Runtime.Loader": "4.0.0",
    "Microsoft.Composition": "1.0.30",
    "System.IO.FileSystem": "4.0.1",
    "System.Collections.NonGeneric": "4.0.1",
    "System.ComponentModel.Primitives": "4.1.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": [ "dnxcore50", "portable-net45+win8" ]
    }
  }
}

0 个答案:

没有答案