我創建了對象類列表並從CreateListFromTable方法返回。但總是虛擬方法正在調用並拋出異常。數據未加載到列表中。對象列表無法正確加載?
可能是什麼問題?
public static List<Customer> GetCustomer(string ID)
{
DataTable dt = new DataTable();
try
{
DatabaseGateway da = new DatabaseGateway();
dt = da.QueryForDataTable("proc_GetCustomer", ID);
}
catch (Exception ex)
{
LogMessage(ex.Message + ": " + ex.StackTrace, EventLogEntryType.Error);
}
return CreateListFromTable<Customer>(dt);
}
protected static List<T> CreateListFromTable<T>(DataTable dt) where T : BusinessObject, new()
{
List<T> list = new List<T>();
try
{
if (dt != null)
foreach (DataRow row in dt.Rows)
{
T t = new T();
t.Load(row);
list.Add(t);
}
}
catch (Exception exception)
{
}
return list;
}
/// <summary>
/// Virtual method which should be overriden by inherited types that support loading.
/// </summary>
/// <param name="row"></param>
protected virtual void Load(DataRow row)
{
throw new NotSupportedException("The object of type '" + this.GetType().Name +
"' does not support loading from DataRow.");
}
什麼可能是例外? – GazTheDestroyer
{「'Customer'類型的對象不支持從DataRow加載。」} – James123