IEnumerable<> 我的一个很费事的实现-》画蛇添足
1
public class myClassCollection : IEnumerable<MyClass>2

{3

[Filed]#region [Filed]4

5
private IList<MyClass> myClassItems = new List<MyClass>();6

7
#endregion8

9

[Constructor]#region [Constructor]10

11
public myClassCollection()12

{13
for (int i = 0; i <= 10; i++)14

{15
MyClass my = new MyClass();16
my.Name = "Nmae" + new Random().Next(100).ToString();17
my.Age = new Random().Next(100);18
myClassItems.Add(my);19
} 20
}21

22
#endregion23

24

[property]#region [property]25

26
public IList<MyClass> MyCollection27

{28
get29

{30
return myClassItems;31
}32
set33

{34
myClassItems = value;35
}36
}37

38
#endregion39

40

[index]#region [index]41

42
public MyClass this[int index]43

{44
get45

{46
return (MyClass)myClassItems[index];47
}48
}49

50
#endregion51

52

IEnumerable53

54
public IEnumerator<MyClass> GetEnumerator()55

{56
return new myEnumerator(this);57
}58

59
#endregion60

61

IEnumerable 成员#region IEnumerable 成员62

63
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()64

{65
return new myEnumerator(this);66
}67

68
#endregion69
}1
public class myEnumerator : IEnumerator<MyClass>2

{3

[Filed]#region [Filed]4

5
private myClassCollection myCollection = new myClassCollection();6
private int currentIndex = -1;7
#endregion8

9

[Constructor]#region [Constructor]10

11
public myEnumerator(myClassCollection coll)12

{13
myCollection = coll;14
}15

16
#endregion17

18

[property]#region [property]19

20
public myClassCollection MyCollection21

{22
get23

{24
return myCollection;25
}26
set27

{28
myCollection = value;29
}30
}31

32
public int CurrentIndex33

{34
get35

{36
return currentIndex;37
}38
set39

{40
currentIndex = value;41
}42
}43

44
#endregion45

46

IEnumerator47

48
public MyClass Current49

{50
get51

{52
return (MyClass)MyCollection[currentIndex];53
}54
}55

56
#endregion57

58

IDisposable 成员#region IDisposable 成员59

60
public void Dispose()61

{62
}63

64
#endregion65

66

IEnumerator 成员#region IEnumerator 成员67

68
object System.Collections.IEnumerator.Current69

{70

get
{ return MyCollection[currentIndex]; }71
}72

73
public bool MoveNext()74

{75
if (currentIndex < MyCollection.MyCollection.Count-1)76

{77
currentIndex++;78
return true;79
}80

81
return false;82
}83

84
public void Reset()85

{86
currentIndex = 0;87
}88

89
#endregion90
}1
public class MyClass2

{3

[Constructor]#region [Constructor]4
#endregion5

6

[Filed]#region [Filed]7

8
private string _name;9

10
private int _age;11

12
#endregion13

14

[property]#region [property]15

16
public string Name17

{18
get19

{20
return _name;21
}22
set23

{24
_name = value;25
}26
}27

28
public int Age29

{30
get31

{32
return _age;33
}34
set35

{36
_age = value;37
}38
}39

40
#endregion41
}1
public partial class Form1 : Form2

{3
public Form1()4

{5
InitializeComponent();6

7
Thread t = new Thread(new ThreadStart(ChangeLabel));8
t.Start();9
}10

11
private void ChangeLabel()12

{13
foreach (MyClass c in new myClassCollection())14

{15
SetLabelText(c);16
Thread.Sleep(1000);17
}18
}19

20
private void SetLabelText(MyClass myClass)21

{22
// label.Text = number.ToString();23
// Do NOT do this, as we are on a different thread.24

25
// Check if we need to call BeginInvoke.26
if (this.InvokeRequired)27

{28
// Pass the same function to BeginInvoke,29
// but the call would come on the correct30
// thread and InvokeRequired will be false.31
this.BeginInvoke(new SetLabelTextDelegate(SetLabelText),32

new object[]
{ myClass });33

34
return;35
}36

37
label1.Text = myClass.Name;38
}39

40
private delegate void SetLabelTextDelegate(MyClass myClass);41
}

浙公网安备 33010602011771号