如何在另一个接口的方法中使用接口或抽象类作为“out”参数?我不应该能够将一个接口用作另一个接口中的输出参数,然后在我实际调用该方法时让它接受任何实现该接口的类吗?
我需要一个事务接口,它有一个返回布尔值并填充“响应”对象的方法,但该响应对象是事务接口的每个不同实现的不同派生对象。提前致谢。
namespace csharpsandbox
{
class Program
{
static void Main(string[] args)
{
TransactionDerived t = new TransactionDerived();
t.Execute();
}
}
public interface ITransaction
{
bool Validate(out IResponse theResponse);
}
public interface IResponse { }
public class ResponseDerived : IResponse
{
public string message { get; set; }
}
public class TransactionDerived : ITransaction
{
public bool Validate(out IResponse theResponse) {
theResponse = new ResponseDerived();
theResponse.message = "My message";
return true;
}
public void Execute()
{
ResponseDerived myResponse = new ResponseDerived();
if (Validate(out myResponse))
Console.WriteLine(myResponse.message);
}
}
}