下面是我的班级。我需要让它成为可枚举的。我在网上看了看,虽然我找到了很多文档,但我还是迷路了。我想这绝对是我第一次问这个问题,但是有没有人能帮我把这个该死的东西列出来,我会从那里弄明白的。我使用的是C# ASP.Net 4.0
public class ProfilePics
{
public string status { get; set; }
public string filename { get; set; }
public bool mainpic { get; set; }
public string fullurl { get; set; }
}发布于 2014-06-26 06:51:50
好吧..。如果你只想让某人“让这个该死的东西变得可枚举”,那就来吧……
public class ProfilePics : System.Collections.IEnumerable
{
public string status { get; set; }
public string filename { get; set; }
public bool mainpic { get; set; }
public string fullurl { get; set; }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
yield break;
}
}它没有枚举任何东西,但它是可枚举的。
现在我会试着读心术,想知道你是否想要这样的东西:
public class ProfilePicture
{
public string Filename { get; set; }
}
public class ProfilePics : IEnumerable<ProfilePicture>
{
public List<ProfilePicture> Pictures = new List<ProfilePictures>();
public IEnumerator<ProfilePicture> GetEnumerator()
{
foreach (var pic in Pictures)
yield return pic;
// or simply "return Pictures.GetEnumerator();" but the above should
// hopefully be clearer
}
}发布于 2012-03-20 13:39:57
问题是:您想枚举什么?
我猜您需要某种容器,其中包含ProfilePics类型的项,因此请使用
List<ProfilePics>或者在如下所示的类中:
public class ProfilePic
{
public string status { get; set; }
public string filename { get; set; }
public bool mainpic { get; set; }
public string fullurl { get; set; }
}
public class ProfilePics : IEnumerable<ProfilePic>
{
private pics = new List<ProfilePic>();
// ... implement the IEnumerable members
}或者只是在这些地方简单地使用List<ProfilePics>,你需要容器。
如果你错过了它:这里是这个IEnumerable的MSDN文档(还有更多的例子)
发布于 2012-03-20 13:40:40
要成为可枚举类,应该实现一些集合。在您的示例中,我没有看到任何集合属性。如果你想做一个头像的集合,把你的类重命名为'ProfiePic‘并使用List。
如果要将某些属性公开为集合,请将其类型设置为IEnumerable、List或其他集合。
https://stackoverflow.com/questions/9781782
复制相似问题