我正在学习C#,对我来说还不清楚如何在foreach
类的FieldsList
循环中访问属性。无法访问。我尝试获取名称为FieldData
的字符串连接。
怎么了?
namespace MyNamesp
{
public class FldData
{
public string Name { get; set; }
public Type FldType { get; set; }
public FldData() { }
public FldData(string name, Type fldType)
{
Name = name;
FldType = fldType;
}
}
class FieldsList<FldData> : List<FldData>
{
public int NumField { get { return this.Count; } }
public string QryFieldList()
{
string _QryFieldList = "";
foreach(FldData fld in this)
{
_QryFieldList += fld.Name +","; //Fld.Name is not accessible
}
return _QryFieldList;
}
}
}
答案 0 :(得分:2)
FieldsList
类的定义不应包含泛型类型参数,
只需在下面声明即可。
在定义为FieldsList<FldData>
时,FldData
部分被视为通用类型参数,它不是FldData
类,尽管两个名称相似。
class FieldsList : List<FldData>
{
public int NumField { get { return this.Count; } }
public string QryFieldList()
{
string _QryFieldList = "";
foreach (FldData fld in this)
{
_QryFieldList += fld.Name + ",";
}
return _QryFieldList;
}
}