簡體   English   中英

將MEF進口與出口匹配

[英]Matching MEF imports to exports

在下面的代碼中,我嘗試使用MEF將導入與匹配的導出進行匹配。 TestMEFClass具有導入和導出,它們共享匹配的合同名稱。 每次調用導出時,都應增加一個計數器。

當我將導出打印到控制台時,它沒有增加計數器。 有人可以指出我的錯誤嗎?

非常感謝你,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace MEFConsoleTest {

    public class TestMEFClass {

        /// <summary>
        /// This counter should increment everytime the getter in the ExportString property gets called.
        /// </summary>
        private int counter = 0;

        [Export("Contract_Name")]
        public string ExportString {

            get {
                return "ExportString has been called " + counter++.ToString();
            }
        }

        [Import("Contract_Name")]
        public string ImportString { get; set; }

        /// <summary>
        /// Default Constructor.
        /// Make a catalog from this assembly, add it to the container and compose the parts.
        /// </summary>
        public TestMEFClass() {

            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }


    }

    class Program {

        static void Main(string[] args) {

            TestMEFClass testClass = new TestMEFClass();
            Console.WriteLine(testClass.ImportString);
            Console.WriteLine(testClass.ImportString);
            Console.ReadLine();

        }
    }

由於種種原因,我目前無法解釋,我無法使MEF和屬性導入/導出可用於可變屬性。 但是,使用函數可以達到目的。 我希望這段代碼對其他人有幫助。

謝謝,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace MEFConsoleTest {

    public class TestMEFClass {

        /// <summary>
        /// This counter should increment everytime the getter in the ExportString property gets called.
        /// </summary>
        private int counter = 0;

        [Export("Contract_Name")]
        string ExportMethod() {
            return ExportString;
        }


        public string ExportString {

            get {
                return "ExportString has been called " + counter++.ToString();
            }
        }

        [Import("Contract_Name")]
        Func<string> ImportMethod;

        public string ImportString { get { return ImportMethod(); } }

        /// <summary>
        /// Default Constructor.
        /// Make a catalog from this assembly, add it to the container and compose the parts.
        /// </summary>
        public TestMEFClass() {

            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }


    }

    class Program {

        static void Main(string[] args) {

            TestMEFClass testClass = new TestMEFClass();

            for (int x = 0; x < 10; x++) {
                Console.WriteLine(testClass.ImportString);
            }

            Console.ReadLine();

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM