【发布时间】:2016-05-21 15:14:03
【问题描述】:
是否可以在另一个类中使用来自特定类的模板?
例子:
class DBObject<DataType>
{
private string _name;
private DataType _value;
public DBObject()
{
_name = String.Empty;
}
public DBObject(string name)
{
this._name = name;
}
public DataType GetValue
{
get { return _value; }
set { _value = value; }
}
}
DataType 是我在 DBObject 类中使用的模板
我的TestTbl 类包含DBObject<DataType> 元素。
class TestTbl : DBTableObjects
{
public DBObject<int> _id;
DBObject<string> _name;
DBObject<string> _address;
TestTbl()
{
AddDBTableObject(_id = new DBObject<int>("id"));
}
}
AddDBTableObject 函数将元素添加到DBTableObjects 类中的List。
class DBTableObjects
{
List<DBObject> ls;
public DBTableObjects()
{
ls = new List<DBObject>();
}
protected void AddDBTableObject(DBObject obj)
{
ls.Add(obj);
}
}
问题:List<DBObject> 还需要 DBObject 实例的模板类型。如何在此处使用 DBObject 类中的 DataType?
【问题讨论】:
-
为什么不使用 DBTableObjects
?