考虑这个通用类:
public class Request<TOperation>
where TOperation : IOperation
{
private TOperation _operation { get; set; }
public string Method { get { return _operation.Method; } }
public Request(TOperation operation)
{
_operation = operation;
}
}
与下面的非通用版本相比,上面的通用版本有什么真正的好处?
public class Request
{
private IOperation _operation { get; set; }
public string Method { get { return _operation.Method; } }
public Request(IOperation operation)
{
_operation = operation;
}
}
IOperation
界面是:
public interface IOperation
{
string Method { get; }
}