C# C IEnumerable不允许访问子项
长话短说。 我有以下代码:C# C IEnumerable不允许访问子项,c#,ienumerable,C#,Ienumerable,长话短说。 我有以下代码: class MyList : IEnumerable { private List<string> T1 = new List<string>(); private List<string> T2 = new List<string>(); private List<string> T3 = new List<string>(); public List<st
class MyList : IEnumerable
{
private List<string> T1 = new List<string>();
private List<string> T2 = new List<string>();
private List<string> T3 = new List<string>();
public List<string> Name { set { T1 = value; } get { return T1; } }
public List<string> DataType { set { T2 = value; } get { return T2; } }
public List<string> Nullable { set { T3 = value; } get { return T3; } }
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public MyList<List<string>, List<string>, List<string>> GetEnumerator()
{
return new MyList<List<string>, List<string>, List<string>>(T1, T2, T2);
}
}
它不允许我访问子项,如item.Name或item.DataType。似乎您正在尝试创建一个事物列表,每个事物都有3个属性:Name、DataType、Nullable。试试这个:
class MyItem
{
public string Name { get; set; }
public string DataType { get; set; }
public string Nullable { get; set; } // also, consider making this bool
}
然后列一个清单 根据@CSJ的回答,我建议:
public class MyItem
{
public string Name { get; set; }
public string DataType { get; set; }
public string Nullable { get; set; } // also, consider making this bool
}
public class MyItemList : IEnumerable<MyItem>
{
private List<MyItem> MyItems = new List<MyItem>();
public IEnumerator<MyItem> GetEnumerator()
{
return this.MyItems.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
请向上投票@CSJ,因为这是基于他的想法 如果你真的想公开你描述的3个列表共同代表一组项的功能,你可以这样做,但是为了你的枚举器的利益,你仍然需要公开一个新的类型
class MyItem
{
public string Name { get; set; }
public string DataType { get; set; }
public bool Nullable { get; set; }
}
class MyList : IEnumerable<MyItem>
{
public List<string> Names { get; set; }
public List<string> DataTypes { get; set; }
public List<bool> Nullables { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<MyItem> GetEnumerator()
{
// assuming all lists are the same length
for (int i = 0; i < Names.Count; i++)
yield return new MyItem {
Name = Names[i],
DataType = DataTypes[i],
Nullable = Nullables[i]
};
}
}
提供的代码会产生大量编译错误。这就是问题所在吗?或者您还有其他问题吗?可能您想要实现IEnumerable。但我认为你有更大的问题。也许您需要一个类对象来表示一个名称、数据类型和可空集。@Styxxy代码没有任何编译问题,我只想能够像问题下面的一小段代码一样访问它。@ChrisSinclair您介意解释一下IEnumerable吗?@Johnny:对不起,我不能早点回答你的问题。主要总结了我当时的想法。OP不太可能真正需要实现自定义IEnumerable,而不仅仅是使用列表。@HighCore:+1 True,以防OP需要更多封装在MyItemList类中的逻辑。@HOKBONG MyItem是否可能没有自己的类?如果您的目的是枚举List的3个列表,则需要MyItemList类。你到底想在这里实现什么?@HOKBONG我想实现的有3个变量name、datatype…等等,每个变量都包含一个列表,也就是说,我希望能够使用foreach获取所有3个列表,请看我在问题中提供的foreach语句。谢谢你replying@CJS我想做的是在类中有3个列表,如果你愿意的话,它就像一个定制的元组。你正在实现一个定制的元组列表。你的理想示例使用说明了这一点。与使用3个平行列表来实现T1[0]、T2[0]和T3[0]共同描述一个对象的目标不同,您应该以各自的类型收集这三个属性。列一个清单就很简单了。非常感谢,这很有帮助,顺便说一句,这实际上是一个错误:只是忘了更改它。再次感谢
class MyItem
{
public string Name { get; set; }
public string DataType { get; set; }
public bool Nullable { get; set; }
}
class MyList : IEnumerable<MyItem>
{
public List<string> Names { get; set; }
public List<string> DataTypes { get; set; }
public List<bool> Nullables { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<MyItem> GetEnumerator()
{
// assuming all lists are the same length
for (int i = 0; i < Names.Count; i++)
yield return new MyItem {
Name = Names[i],
DataType = DataTypes[i],
Nullable = Nullables[i]
};
}
}
MyList ml = new MyList();
ml.Names = new [] { "AccountNum", "Value", "Owner" } .ToList();
ml.DataTypes = new [] { "nvarchar(50)", "decimal(14,6)", "nvarchar(50)" } .ToList();
ml.Nullables = new [] { false, false, true } .ToList();
foreach (var item in ml)
{
str = item.Name;
}