.net - 接口(interface)中的可选方法

标签 .net interface

我需要更改由多个应用程序使用的界面。只有我正在开发的这个应用程序才需要更改。我需要向该方法添加更多参数。

我确实有权更改其他应用程序中的代码,只需几分钟即可更改其他应用程序以使用正确的方法签名。然后我可以让这些应用程序不使用这些额外参数。

或者我可以创建一个重载并让其他应用程序实现这些方法,我猜什么也不做。或者让他们在没有额外参数的情况下调用原始方法。

friend 们,这里的最佳实践是什么?

最佳答案

您可以添加实现旧接口(interface)(即 IOldInterface)的新接口(interface)(即 INewInterface)。然后将 INewInterface 实现到类/结构。

public interface IOldInterface {
    object DoSomething( int number );
}
...
public interface INewInterface : IOldInterface {
    object DoSomething( int number, TimeSpan time );
}
...
public class MyClass : IOldInterface, INewInterface {
    public object DoSomething( int number ) { ... }
    public object DoSomething( int number, TimeSpan time ) { ... }
}

关于.net - 接口(interface)中的可选方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5475864/

相关文章:

c# - IIS 7.5 上经典 ASP 中 WshShell.Exec 的 System.IO.FileLoadException

c++ - 隐藏C++接口(interface)的具体实现

java - 使用接口(interface)而不是类

javascript - TS |元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 'Record<SecurityMode, boolean>'

go - 在 Golang 中传递 interface{} 或 []interface{}

c# - 比较图像作为单元测试的字节数组

c# - 如何从不同的输入插入

.net - 使用特定 DLL 库时内存分配非常高。或许。不知道该怎么办

c# - 在 HashSet<T> 上调用 Distinct<>()

c# - 我可以忽略 C# 接口(interface)中的泛型类型吗?