这种用法,使用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();
}
}
对应源代码:源代码