c#求指点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()就可以了
}
------解决方案--------------------
我再举例说明白点 ,
我做了3个 工程 A,B,C 。 A和B 是我做的共通类 比如控件之类的 。 C是我的主程序 。 我在C#.NET里面添加了 这3个工程 。然后我把C工程 添加引用(ADD REFERENCE)A和B。 然后我就可以调用A和B的东西了,编译生成C的EXE文件,那A和B 就是其中的DLL文件了 。
想明白了的话,也就容易理解上面 各位大哥的话了。