使用没有抽象的 Base 方法

Using a Base method which has no abstraction

下面的代码可以吗?我在问自己接口 ICustomTimer 中的 Start()Stop() 方法是否适合作为位置。因为我的 Main 方法中需要它。

这是代码异味,或者换句话说,调用没有抽象的基本方法的最佳做法是什么? Timer class 没有我可以用来继承的接口。

public interface ICustomTimer
{
    string Value { get; set; }

    //Implementation in Timer
    void Start();

    //Implementation in Timer
    void Stop();
}

public class CustomTimer : System.Timers.Timer, ICustomTimer
{
    public string Value { get; set; }
}

public Main()
{
    var customTimerObj = iocContainer.Get<ICustomTimer>();
    customTimerObj.Start();
}

这是一个有效的使用,甚至是一个很好的使用,如果您只是在使用接口时调用 class 方法,否则您可以这样做:

public interface ICustomTimer
{
    string Value { get; set; }

    //Implementation in Timer
    void Start();

    //Implementation in Timer
    void Stop();
}

public class CustomTimer : System.Timers.Timer, ICustomTimer
{
    public string Value { get; set; }
    void ICustomTimer.Start() { this.Start(); }
    void ICustomTimer.Stop() { this.Stop(); }
}

这样你就可以做其他事情了(先于或post调用定时器的方法class)