c#求指导DLL 文件中类的继承
c#求指点DLL 文件中类的继承
如果我有个主程序里面有 5个类 class A,B,C,D,E,还有一个类E,但是E 是从D继承过来的,我想把E写成封装的DLL 文件,然后由外部载入使用Assembly动态加载 。可是D的这个类跟A.B.C都有关系,E要如何写呢?
------解决方案--------------------
假设一开始你的主程序是这样的
DoSomething(D obj)这里用到了D和E的一些共性的东西,所以你可以从这里入手,抽出一个接口,改成:
独立的dll
主程序引用dll
------解决方案--------------------
如果我有个主程序里面有 5个类 class A,B,C,D,E,还有一个类E,但是E 是从D继承过来的,我想把E写成封装的DLL 文件,然后由外部载入使用Assembly动态加载 。可是D的这个类跟A.B.C都有关系,E要如何写呢?
c#
------解决方案--------------------
假设一开始你的主程序是这样的
public class D
{
public A a { get; set; }
public B b { get; set; }
public C c { get; set; }
}
public class E : D {}
......
static void Main()
{
Console.WriteLine(DoSomething(new E()));
Console.WriteLine(DoSomething(new D()));
}
static string DoSomething(D obj)
{
......
}
DoSomething(D obj)这里用到了D和E的一些共性的东西,所以你可以从这里入手,抽出一个接口,改成:
独立的dll
public interface IDoSomething
{
public string DoSomething();
}
public class E : IDoSomething
{
...... <- 把实现接口的时候需要用到的东西放过来
public string DoSomething()
{
...... <- 具体干活,参考以前的DoSomething(D obj)
}
}
主程序引用dll
public class D : IDoSomething
{
...... <- 和以前一样
public string DoSomething()
{
...... <- 具体干活,参考以前的DoSomething(D obj)
}
}
static void Main()
{
Console.WriteLine(DoSomething(new E()));
Console.WriteLine(DoSomething(new D()));
}
static string DoSomething(IDoSomething obj) <- 传入的是接口,所以D和E都能用
{
...... <- 不用管obj具体是什么,只要拿到obj.DoSomething()就可以了
}
------解决方案--------------------