0
我试图创造一个interface
这样的接口中声明的方法:如何在未知的数量和类型参数
public interface IProcessable<out T>
{
T Result { get; }
T Execute(params object[] args);
}
我想有一个Execute()
方法返回T
和能够接受任何类型的参数。
我想用这样的:
public class CustomProcess: IProcessable<int>
{
public int Result { get; private set; }
public int Execute(string customArg)
{
/// some codes to return int
}
}
IProcessable<int>
将迫使我补充一个Execute
方法是这样的:
public int Execute(params object[] args)
{
var customArg= args[0] as string;
if (customArg!= null) //I know that this is not so necessary for `string`
{
return Execute(customArg);
}
throw new Exception();
}
我想,我应该有,因为宽CustomProcess
两种方法object[]
的可接受范围。
知道我应该问:
这种接口是可以接受的还是一种反模式?
有没有什么更好的方法来实现我想要的 - 创建一个强制类具有Result
属性和Execute
方法的接口 - ?
它甚至没有意义首先要对为数众多的*都有着不同的签名不同的方法的接口*。这破坏了接口的全部目的。 *要么确保所有的方法都可以使用相同的签名*,要么*不使用接口*,因为您没有得到任何东西。 – Servy