C#中MEF中Import和Export使用

这种用法,使用export是按照类、方法等导出的,不是实例

1、定义基础类

    public abstract class Person
    {
        public int Age = 0;
        public string Name = "";

        public Person()
        { 
        
        }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        } 
    }

    public interface IGetPersonInfo
    {
        string GetPersonInfo();
    }

定义Doctor、Teacher

    public class Teacher : Person, IGetPersonInfo
    {
        public string SchoolName = "";

        public Teacher() : base()
        { 
        
        }

        public Teacher(string name,int age, string schoolName) : base(name,age)
        {
            SchoolName = schoolName;
        }
        public string GetPersonInfo()
        {
            return $"Teacher:{Name},{Age},{SchoolName}";
        } 
    }

    public class Doctor : Person, IGetPersonInfo
    {
        public string HospitalName = "";

        public Doctor() : base()
        {

        }

        public Doctor(string name, int age, string hospitalName) : base(name, age)
        {
            HospitalName = hospitalName;
        }

        public string GetPersonInfo()
        {
            return $"Doctor:{Name},{Age},{HospitalName}";
        }
    }

再定义person管理类

    public interface IManage
    {
        List<string> GetNameLt();

        void AddPerson(Person theP);

        IGetPersonInfo GetIPerson(string theName);
    }

    [Export("rqName1", typeof(IManage))]
    public class PersonManage : IManage
    {
        static List<Person> glPersonLt = new List<Person>();

        public PersonManage()
        {

        }

        public List<string> GetNameLt()
        { 
            var lt = glPersonLt.Select( (ele) => { return ele.Name; }).ToList();

            return lt;
        }

        public void AddPerson(Person theP)
        {
            glPersonLt.Add(theP); 
        }
         
        public IGetPersonInfo GetIPerson(string theName)
        {
            IGetPersonInfo theI = null;

            Person theP = glPersonLt.Find(ele => ele.Name == theName);
            if (theP == null) return theI;
             
            switch (theP) {
                case Teacher tempP: {
                        theI = tempP;
                        break;
                    }
                case Doctor tempP: {
                        theI = tempP;
                        break;
                    }
            }
             
            return theI;

        }
    }

2、在主程序中调用使

先定义导入类

    public class SysCla
    {
        [Import("rqName1", typeof(IManage))]
        public IManage glIManage { get; set; }
    }

主程序中,Init函数和Run分别用导入方法,进行操作

public partial class Form1 : Form
    { 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            
        }

        private void 初始化_Click(object sender, EventArgs e)
        {
            //初始化
            Init();
        }

        public void Init()
        {
            SysCla theSys = new SysCla();

            //1、用当前代码的程序集,初始化部件目录类(可以把本程序中定义的GDCard加载进来)
            var nowCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            //2、用指定的类型,初始化部件目录类
            //var specificCatalog = new AssemblyCatalog(typeof(GDCard).Assembly);

            //3、使用指定目录中的dll,初始化部件目录类(可以把指定.dll中的各种加载进来)
            //string path = "./";
            //path = AppDomain.CurrentDomain.BaseDirectory;
            //var directoryCatalog = new DirectoryCatalog(path, "*.dll");

            //==================合并以上多个部件目录类
            //var combinCataLog = new AggregateCatalog(nowCatalog, specificCatalog, directoryCatalog);
            //如果只用其中某一个,会对应
            var combinCataLog = new AggregateCatalog(nowCatalog);

            CompositionContainer container = new CompositionContainer(combinCataLog);

            //对象的导入
            container.SatisfyImportsOnce(theSys);
             
            if (theSys.glIManage != null) {
                theSys.glIManage.AddPerson(new Doctor("rcy", 5, "医院1"));
                theSys.glIManage.AddPerson(new Teacher("gxj", 35, "学校1")); 
            }


            //释放资源
            container.Dispose();
        }

        public void Run()
        {
            SysCla theSys = new SysCla();

            //1、用当前代码的程序集,初始化部件目录类(可以把本程序中定义的GDCard加载进来)
            var nowCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            //2、用指定的类型,初始化部件目录类
            //var specificCatalog = new AssemblyCatalog(typeof(GDCard).Assembly);

            //3、使用指定目录中的dll,初始化部件目录类(可以把指定.dll中的各种加载进来)
            //string path = "./";
            //path = AppDomain.CurrentDomain.BaseDirectory;
            //var directoryCatalog = new DirectoryCatalog(path, "*.dll");

            //==================合并以上多个部件目录类
            //var combinCataLog = new AggregateCatalog(nowCatalog, specificCatalog, directoryCatalog);
            //如果只用其中某一个,会对应
            var combinCataLog = new AggregateCatalog(nowCatalog);

            CompositionContainer container = new CompositionContainer(combinCataLog);

            //对象的导入
            container.SatisfyImportsOnce(theSys);
             
            if (theSys.glIManage != null) {
                var aI = theSys.glIManage.GetIPerson("rcy");
                string bb = aI.GetPersonInfo();
                Console.WriteLine(bb);
            }


            //释放资源
            container.Dispose();
        }

        private void 运行_Click(object sender, EventArgs e)
        {
            //运行
            Run();
        }
    }

对应源代码:源代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值