[英]Create a Generic list of <T>
I want to create a method that will create a List of generic objects.我想创建一个方法来创建一个通用对象列表。 Is it possible?
可能吗? Something like this:
像这样的东西:
public class Mtrl
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
}
public class Trdr
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Address { get; set; }
public string AFM { get; set; }
public string Phone01 { get; set; }
public string Aa { get; set; }
}
And then with a generic class to create my list:然后使用通用 class 创建我的列表:
public class GenericClass<T>
{
public List<T> GetData<T>()
{
List<T> myList = new List<T>();
if (typeof(T) == typeof(Trdr))
{
myList.Add(new Trdr());//Error 1: cannot convert from Trdr to 'T'
}
if (typeof(T) == typeof(Mtrl))//Error 2: cannot convert from Mtrl to 'T'
{
myList.Add(new Mtrl());
}
return myList;
}
}
My mistake.我的错。 I will try to clarify more.
我将尝试澄清更多。 The Classes Trdr,Mtrl etc will have many different properties.
Trdr、Mtrl 等类将具有许多不同的属性。 The Getdata method will take data from a web service via json and i want to create a List of Objects and return it Something like this:
Getdata 方法将通过 json 从 web 服务获取数据,我想创建一个对象列表并返回它是这样的:
public List<T> GetData<T>()
{
List<T> myList = new List<T>();
if (typeof(T) == typeof(Trdr))
{
for (int i = 0; i < 100; i++)//fetch data from web api in json format
{
Trdr NewObj = new Trdr();
NewObj.Aa = "...";
NewObj.AFM = "...";
myList.Add(NewObj);
}
}
if (typeof(T) == typeof(Mtrl))
{
for (int i = 0; i < 100; i++)
{
Mtrl NewObj = new Mtrl();
NewObj.Aa = "...";
NewObj.Name = "name ...";
myList.Add(NewObj);
}
}
return myList;
}
}
I suggest extracting an interface (or even a base class ):我建议提取一个接口(甚至是一个基本的 class ):
//TODO: Since you have more than 2 classes, please check the common interface
public interface Idr {
string Id { get; set; }
string Name { get; set; }
string Code { get; set; }
string Aa { get; set; }
}
With all classes of interest implementing it:所有感兴趣的类别都实现它:
public class Mtrl : Idr
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
}
public class Trdr : Idr
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
public string Address { get; set; }
public string AFM { get; set; }
public string Phone01 { get; set; }
}
Now you can use List<Idr>
collection;现在您可以使用
List<Idr>
集合; we want the class ( T
) that implements Idr
to have a parameterless constructor as well ( new()
):我们希望实现 Idr 的
Idr
( T
) 也具有无参数构造函数 ( new()
):
public class GenericClass<T> where T : Idr, new()
{
public List<T> GetData()
{
List<T> myList = new List<T>() {
new T();
};
return myList;
}
}
Sounds like you are trying to create a new list that contains some initial data, and you need to do this for many different types T
.听起来您正在尝试创建一个包含一些初始数据的新列表,并且您需要对许多不同类型的
T
执行此操作。
If Mtrl
and Trdr
has nothing in common and are handled completely differently, consider simply using different methods to get each type of list:如果
Mtrl
和Trdr
没有任何共同点并且处理方式完全不同,请考虑简单地使用不同的方法来获取每种类型的列表:
GetDataMtrl(){
var result = new List<Mtrl>();
result.Add(new Mtrl());
return result;
} // etc
Otherwise what you need is Type Constraints of Generic Parameters , that can tell the compiler more about what T
is.否则,您需要的是通用参数的类型约束,它可以告诉编译器更多关于
T
是什么的信息。 Or rather what the different values of T
must have in common.或者更确切地说,
T
的不同值必须有什么共同点。 For example you can do例如你可以做
public List<T> GetData<T>() where T : new()
{
List<T> myList = new List<T>();
myList.Add(new T());
return myList;
}
To say that for T
it has to be possible to do new T()
.要说对于
T
必须可以做new T()
。 Then you can say GetData<Trdr>()
to get a list that contains a single empty Trdr
, as you code might seem to try to do.然后,您可以说
GetData<Trdr>()
以获取包含单个空Trdr
的列表,就像您的代码似乎尝试做的那样。
Maybe you need to set some default values in the data.也许您需要在数据中设置一些默认值。 I notice the classes has a lot of variables in common.
我注意到这些类有很多共同的变量。 Then you might consider using inheritance to specify this commonality:
那么你可以考虑使用 inheritance 来指定这个共性:
public class Mtrl
{
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Aa { get; set; }
}
public class Trdr : Mtrl
{
public string Address { get; set; }
public string AFM { get; set; }
public string Phone01 { get; set; }
}
And then write generic code where T
is either of the type Mtrl
or its descendant class:然后编写通用代码,其中
T
是Mtrl
类型或其后代 class:
public List<T> GetData<T>() where T : Mtrl
{
List<T> myList = new List<T>();
T MtrlOrTrdr = new T();
MtrlOrTrdr.Id = "my-new-id-";
myList.Add(MtrlOrTrdr);
return myList;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.