示例#1
0
        static void MefTest(string pa)
        {
            pa += "..\\";
            var a = new MyClass();

            if (Find(pa, a))
            {
                foreach (var item in a.Cals)
                {
                    var temp = item.GetOperations();
                }
            }
            var b = new MyCal();

            FindExt <MyCal, ICalShow>(pa, b);
            var s = b.Show?.Show(5);
            var c = new MCal2();

            if (Find(pa, c))
            {
                var res3  = c.MDefinC.GetStr();
                var res4  = c.Subtract[0].Value(0.3, 0.4);
                var resi3 = c.Add[0](90, 90);
                var s1    = "1";
                var s2    = "2";
                c.RefeObj(s1, s2);
            }
        }
示例#2
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);
        }