[英]Create a generic function that return a generic type list in C#
I have two Model
classes, one is Person
and the other is Department
. 我有两个
Model
类,一个是Person
,另一个是Department
。 What I want to achieve is I want to create a generic function which can be used to retrieve generic type data in the form of List
for eg one call will return Person List
, another call will return Department List
. 我要实现的是创建一个通用函数,该函数可用于以
List
的形式检索通用类型数据,例如,一个调用将返回Person List
,另一个调用将返回Department List
。 More specifically I want to return data in the form of a List. 更具体地说,我想以列表的形式返回数据。
The below is the Person
Class. 以下是
Person
类别。
public class Person {
public Int16 Personid{ get; set; }
public string Personname { get; set; }
public string Personaddress { get; set; }
}
The below is the Department
class. 以下是
Department
课程。
public class Department {
public Int16 Departmentid { get; set; }
public string Departmentname { get; set; }
public string Departmentsection { get; set; }
}
The below are the calling function where One time I make a call with CallingMethodPerson()
and other with CallingMethodDepartment()
. 下面是调用函数,一次我使用
CallingMethodPerson()
进行调用,而另一次使用CallingMethodDepartment()
进行调用。
public class CallingClass {
public void CallingMethodPerson() {
CalledClass calling = new CalledClass();
Calling. CalledMethod();
}
public void CallingMethodDepartment() {
CalledClass calling = new CalledClass();
Calling. CalledMethod();
}
}
The below blue print is the CalledClass
where it does some manipulation and return List of either Person
or Department
. 下面的蓝图是
CalledClass
,它在其中进行一些操作并返回Person
或Department
List。
public class CalledClass {
public void CalledMethod() {
//this is a generic method wherein returns the list of either a Person
or Department whenever called.
}
}
To summarise I want to implement a common function that return generic type data. 总而言之,我想实现一个返回通用类型数据的通用函数。 The trick is actually I want build one generic function that has a
SQL Read command
, so I will call the same function again and again with different models, execute the query, retrieve the data from the database
, store it into a list
of type generic and return back to the callingmethod
. 诀窍是实际上我想构建一个具有
SQL Read command
泛型函数,因此我将使用不同的模型反复调用同一函数,执行查询,从database
检索数据,并将其存储到泛型类型list
中然后返回到callingmethod
。
Your CalledClass should accept T as generic. 您的CalledClass应该接受T作为泛型。
Both your Person and Department should have atleast and Interface in common (if you need to do things with those objects.) 您的个人和部门都应该有至少相同的至少接口(如果您需要使用这些对象来做事)。
public class CalledClass<T> where T : IAmAClassObject {
public List<T> CalledMethod() {
//create your objects and do something
return objects.ToList();
}
}
Another way of handling this is to make the CalledMethod handle the generic public class CalledClass { 解决此问题的另一种方法是使CalledMethod处理通用的公共类CalledClass {
public List<T> CalledMethod<T>() {
//create your objects and do something
return objects.ToList();
}
}
This way you should instantiate the CalledClass once in the CallingClass class, and just call the CallMethod giving the right T type. 这样,您应该在CallingClass类中实例化一次CalledClass,然后调用提供正确T类型的CallMethod。
Looks like you could benefit from entity framework. 看起来您可以从实体框架中受益。 It has the method you need, it is called using
DbContext
, DbSet<T>
and Linq-to-sql. 它具有您需要的方法,使用
DbContext
, DbSet<T>
和Linq-to-sql进行调用。
The correct syntax to do this is the following: 正确的语法如下:
public class CalledClass<T>
{
public List<T> CalledMethod()
{
return objects.ToList();
}
}
But this will not allow you to modify the list entries, because there are of type object
. 但这将不允许您修改列表条目,因为存在类型为
object
。
Tour example shows that your objects are quite similar, so i would suggest extracting a common interface: 巡回示例显示您的对象非常相似,因此我建议提取一个公共接口:
Person
and Department
this information is already given with the class Person
和Department
此类已经提供了该信息 This will leave you with the following hierarchy 这将使您具有以下层次结构
public interface IEntity
{
string Name {set;get;}
string Id {set;get;}
}
public class Person : IEntity
{
string Name {set;get;}
string Id {set;get;}
string Address {set;get;}
}
public class Department : IEntity
{
string Name {set;get;}
string Id {set;get;}
string Section {set;get;}
}
You can now add a constraint that your generic function only takes a child of IEntity
现在,您可以添加一个约束,使您的泛型函数仅接受
IEntity
public class CalledClass<TEntity>
where TEntity : IEntity
{
public List<TEntity> CalledMethod()
{
// You can access Name and Id here
foreach(var entity in entities) // i know the code is not useful ;)
entity.Id = "Unique ID"; // only for demonstration purposes
entity.Name = "Random Name";
}
return entities.ToList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.