考慮這個泛型類:C#泛型 - 通過使包裝類具有通用性而獲得了什麼?
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; }
}
Gotcha - 所以換句話說,它允許您在需要消費的位置更加嚴格地「關閉」或指定「請求」類型,但無需爲不同的操作類型創建自定義的「請求」對象。 – MalcomTucker 2013-02-12 15:08:05
@ MalcomTucker:正確。 – 2013-02-12 15:10:39