我打電話有兩個不同的類,如下一個通用的方法:類屬性是不通用的方法可用的C#
FillDataPointsInOrder<Metrics>(dataPoints.Where(O => O.SortOrder != null).OrderBy(O => O.SortOrder));
FillDataPointsInOrder<Metric>(angieStatsCmp.GetDataColumns());
private void FillDataPointsInOrder<T>(IEnumerable<T> dataPoints)
{
foreach (T dpoint in dataPoints)
{
if (!dpoint.IsPhone)
FillDrp(this.EmailDrp, dpoint.Name, dpoint.MetricId.ToString(), dpoint.VName);
if (dpoint.IsPhone && this.IsPhoneShop)
FillDrp(this.PhoneDrp, dpoint.Name, dpoint.MetricId.ToString(), dpoint.VName);
}
}
在「FillDataPointsInOrder」
方法,我收到編譯錯誤:
'T' does not contain a definition for 'IsPhone' and no extension method 'IsPhone' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
Name,MetricId和VName屬性的相同錯誤。 不確定T爲什麼無法訪問Metrics和Metric的屬性。 如果我從泛型方法中刪除代碼,並直接在foreach中直接寫入數據點,那麼它工作正常。
有人可以提醒這裏有什麼問題嗎?
好點。在問題中給出的例子中,如果相關的接口或基類存在,則首先不需要泛型方法。 – Xiaofu