コード例 #1
0
        /// <summary>
        /// 基于约定的部件注册
        /// </summary>
        /// <param name="path"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool FindEx(string path, MyCal obj)
        {
            var res = false;
            var rb  = new RegistrationBuilder();                         //用于定义出口和入口的约定

            rb.ForTypesDerivedFrom <ICalShow>().Export <ICalShow>();     //指定出口约定  给所有实现接口的类型应用[Export(Type)]特性
            rb.ForType <MyCal>().ImportProperty <ICalShow>(t => t.Show); //指定入口约定 将入口映射到指定的属性上
            var dc = new DirectoryCatalog(path, rb);
            CompositionService sv = dc.CreateCompositionService();

            try
            {
                sv.SatisfyImportsOnce(obj, rb); //在目录中搜索部件 多个出口匹配项出异常
                res = true;
            }
            catch (ChangeRejectedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (CompositionException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            sv.Dispose();
            return(res);
        }
コード例 #2
0
        /// <summary>
        /// 基于约定的部件注册 类T注册接口K
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="K"></typeparam>
        /// <param name="path"></param>
        /// <param name="obj"></param>
        /// <param name="propertyFilter"></param>
        /// <returns></returns>
        public static bool FindExt <T, K>(string path, T obj, Predicate <PropertyInfo> propertyFilter)
        {
            var res = false;
            var rb  = new RegistrationBuilder();               //用于定义出口和入口的约定

            rb.ForTypesDerivedFrom <K>().Export <K>();         //指定出口约定  给所有实现接口的类型应用[Export(Type)]特性
            rb.ForType <T>().ImportProperties(propertyFilter); //指定入口约定
            var dc = new DirectoryCatalog(path, rb);
            CompositionService sv = dc.CreateCompositionService();

            try
            {
                sv.SatisfyImportsOnce(obj, rb); //在目录中搜索部件 若存在多个出口匹配项则抛出出异常
                res = true;
            }
            catch (ChangeRejectedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (CompositionException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            sv.Dispose();
            return(res);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: simple555a/DotNetAppDev
        public void Run()
        {
            var conventions = new RegistrationBuilder();

            conventions.ForTypesDerivedFrom <ICalculator>().Export <ICalculator>();
            conventions.ForType <Program>().ImportProperty <ICalculator>(p => p.Calculator);
            var catalog = new DirectoryCatalog(Settings.Default.AddInDirectory, conventions);

            using (CompositionService service = catalog.CreateCompositionService())
            {
                service.SatisfyImportsOnce(this, conventions);
            }

            CalculatorLoop();
        }