我有一個函數,我想要使用泛型返回CreditSupplementTradeline或CreditTradeline。問題是如果我創建一個T ctl = new T(); ...我無法操作ctl,因爲VS2010不能識別它的任何屬性。這可以做到嗎?謝謝。如何實例化,操作並返回類型T
internal T GetCreditTradeLine<T>(XElement liability, string creditReportID) where T: new()
{
T ctl = new T();
ctl.CreditorName = this.GetAttributeValue(liability.Element("_CREDITOR"), "_Name");
ctl.CreditLiabilityID = this.GetAttributeValue(liability, "CreditLiabilityID");
ctl.BorrowerID = this.GetAttributeValue(liability, "BorrowerID");
return ctl;
}
我得到這個錯誤:
Error 8 'T' does not contain a definition for 'CreditorName' and no extension method 'CreditorName' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
完美,這正是我所期待的。謝謝。 – ElMatador