我有兩個類通用C#方法
public class JobDataProvider
{
public List<Job> Get(int id){
List<Job> jobs = new List<Job>();
foreach(up_GetJobResult result in myDataContext.up_GetJob(id))
{
jobs.add(new Job(Id = result.Id, name = result.Name));
}
return jobs;
}
}//end class Job
public class PersondataProvider{
public List<Person> Get(int id){
List<Person> persons = new List<Persons>();
foreach(up_GetPersonsResult result in MyDataContext.up_GetPerson(id)){
persons.add(new Person(Id = result.Id, Name = result.Name, Surname = result.Surname));
}
return persons;
}
}end class Person
我想創建一個通用的方法,這樣的事情
public List<T> Get<T>(int id)
{
.. get data and return the list
}
Job和Person類之間是否有任何關係?他們有共同的基礎班嗎? – 2010-02-25 22:17:53
除非工作與人之間存在明確的關係,並且您更多地重構代碼以提取抽象並且具有構建人員和工作(例如因子)的常見方式,否則我認爲迄今爲止提交的任何答案都不起作用。除了你從數據上下文調用不同的方法,你正在使用不同的參數 – 2010-02-25 23:05:14