1

我有一个我们购买的 DLL,但我无法访问它的源代码。然而,对于我现在面临的问题,我被告知我必须从 DLL 中的一个类继承,并重写它的一个方法。

我试过了,效果很好,正如预期的那样。我现在的问题是我有很多类我需要做同样的事情,并且不想多次重复相同的代码。尽管所有这些类都继承自同一个 DLL 类,但在所有这些类中编写相同的代码对我来说并不好。这是一类的代码:

public class MyClass : DllClass
{
    protected override void MethodFromDll()
    {
       //here I inserted my code and it works ok
    }
}

谢谢

4

1 回答 1

3

为什么不创建一个派生自DllClass并覆盖那里的方法的抽象类,然后从中派生其他类呢?

public abstract class MyClassBase : DllClass
{
    protected override void MethodFromDll()
    {
       //here I inserted my code and it works ok
    }
}

...

public class MyClass : MyClassBase
{
    // Whatever else you need
}

那是假设您首先需要在这里继承-您肯定需要有多个派生类,还是可以有很多MyClass由组合使用的类?

于 2012-10-14T08:49:03.017 回答