我想從一個不同的.dll類中的方法接收的對象實例化一個列表的新實例。當我這樣做,我得到一個類型轉換錯誤:從一個返回列表的方法實例化一個列表
Cannot implicitly convert type System.Collections.Generic.List<HelpDeskBusinessDataObject.Employee> to System.Collections.Generic.List<HelpDeskBusinessUserObject.Employee>
這是怎麼了實例吧:
public List<EmployeeBusinessUser> GetAll()
{
EmployeeBusinessData empData = new EmployeeBusinessData();
List<Employee> employees = new List<Employee>();
List<EmployeeBusinessUser> retEmployees = new List<EmployeeBusinessUser>();
try
{
//Here is where I am trying to get the list assigned to what is
//returned from the method call
employees = empData.GetAll();
}
catch (Exception ex)
{
ErrorRoutine(ex, "EmployeeUserData", "GetAll");
}
return retEmployees;
}
感謝您的幫助。
編輯:GETALL()方法:
public List<Employee> GetAll()
{
HelpDeskDBEntities dbContext = new HelpDeskDBEntities();
List<Employee> employees = new List<Employee>();
try
{
employees = dbContext.Employees.ToList();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return employees;
}
'HelpDeskBusinessDataObject'!='HelpDeskBusinessUserObject' – PSL
@ PSL的建議是正確的。你也嘗試聲明爲var? – Jeyara
我知道。我在名爲EmployeeBusinessData的類中調用一個名爲HelpDeskBusinessDataObjects的項目中的方法。假設我有上面這個方法調用EmployeeBusinessData中的方法,並且能夠返回一個員工對象列表。 – Delete