这是关于泛型的两(2)部分问题
我必须创建几个类似的类来为类似设计的数据库表建模.
所有表都包含一个ID int和一个Text nvarchar(50)字段.一个或两个可能包含一些其他字段.
我很少使用泛型,但我经常在这里看到它的例子.这是我创建另一个泛型类中使用的泛型类的最大尝试.
我的基本结构如下,我将用注释指出什么不起作用以及Visual Studio 2010显示的错误消息:
public class IdText {
public IdText(int id, string text) {
ID = id;
Text = text;
}
public int ID { get; private set; }
public string Text { get; private set; }
}
public class TCollection<T> : IEnumerable<T> where T : IdText {
private List<T> list;
public TCollection() {
list = new List<T>();
}
public void Add(int id, string text) {
foreach (var item in list) {
if (item.ID == id) {
return;
}
}
list.Add(new T(id, text)); // STOP HERE
// Cannot create an instance of the variable type 'T'
// because it does not have the new() constraint
}
public T this[int index] {
get {
if ((-1 < 0) && (index < list.Count)) {
return list[index];
}
return null;
}
}
public T Pull(int id) {
foreach (var item in list) {
if (item.ID == id) {
return item;
}
}
return null;
}
public T Pull(string status) {
foreach (var item in list) {
if (item.Text == status) {
return item;
}
}
return null;
}
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator() {
foreach (var item in list) yield return item;
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return list.GetEnumerator();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio的IntelliSence希望我添加list.Add(T item),但我需要先创建它.
我试图重写违规行,但后来我被list.Add(new T(id, text));称为"无法转换为"list.Add(new IdText(id, text));的消息.IdTextT
我究竟如何解决这个问题?
下一篇:当我IdText稍后进入实际创建该类的一个版本时,我不确定我在TCollection我为它设计的类中如何使用这个新类.
例如,给定此派生类:
public class ManufacturedPart : IdText {
public ManufacturedPart(int id, string partNum, string description)
: base(int id, string partNum) {
Description = description;
}
public string Description { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
......我还需要派出一个特殊版本TCollection来陪伴它吗?
public class ManufacturedParts<T> : IEnumerable<T> where T : ManufacturedPart {
// OK, now I'm lost! Surely this can't be right!
}
Run Code Online (Sandbox Code Playgroud)
1)您可以使用new()约束,公开您的属性并添加无参数构造函数:
public class IdText
{
public IdText()
{
}
public IdText(int id, string text)
{
ID = id;
Text = text;
}
public int ID { get; set; }
public string Text { get; set; }
}
public class TCollection<T> : IEnumerable<T> where T : IdText, new()
{
private List<T> list;
public TCollection()
{
list = new List<T>();
}
public void Add(int id, string text)
{
foreach (var item in list)
{
if (item.ID == id)
{
return;
}
}
list.Add(new T { ID = id, Text = text });
}
}
Run Code Online (Sandbox Code Playgroud)
2)您有多种选择:
如果您希望您的集合存储任何IdText(ManufacturedPart或其他任何派生的IdText):
TCollection<IdText> ss = new TCollection<IdText>();
Run Code Online (Sandbox Code Playgroud)
目前,上述方法只能IdText在Add(int, string)方法中实例化对象时存储,但如果提供Add(T object)方法,则可以存储任何IdText实例.
如果您希望您的收藏仅包含ManufacturedParts:
public class ManufacturedParts<T> : TCollection<T> where T : ManufacturedPart, new()
{
// Provide here some specific implementation related to ManufacturedParts
// if you want. For example, a TotalPrice property if ManufacturedPart
// has a Price property.
}
TCollection<ManufacturedPart> ss2 = new ManufacturedParts<ManufacturedPart>();
Run Code Online (Sandbox Code Playgroud)
或者更简单,如果您的集合不提供任何其他方法,具体取决于存储对象的类型:
TCollection<ManufacturedPart> ss2 = new TCollection<ManufacturedPart>();
Run Code Online (Sandbox Code Playgroud)
更简单的是,如果您的目标只是存储对象,则不需要自定义集合:
List<IdText> ss2 = new List<IdText>(); // Uses the built-in generic List<T> type
Run Code Online (Sandbox Code Playgroud)