欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[.NET] CastingEnumerable

程序员文章站 2022-07-02 18:44:23
前言 : 写程式难免,会遇到要使用自订函式来作物件阵列转型。 写了一个物件来做这个动作,跟大家分享。 说明 : 这个物件的特点为: 1. 使用到才转型。不会在记忆体,建立...

前言 :
写程式难免,会遇到要使用自订函式来作物件阵列转型。
写了一个物件来做这个动作,跟大家分享。

说明 :
这个物件的特点为:
1. 使用到才转型。不会在记忆体,建立整个转型后的阵列副本。
2. 可在转型时加入参数。

使用范例 :
view source
 

01 namespace clk.collections.consoleapplication
02 {
03     public class user
04     {
05         public string lastname = string.empty;
06  
07         public string firstname = string.empty;
08     }
09  
10     public class data
11     {
12         public string displayname = string.empty;
13     }
14  
15     class program
16     {
17         static void main(string[] args)
18         {            
19             user[] userarray = new user[2];
20  
21             userarray[0] = new user();
22             userarray[0].lastname = "chou";
23             userarray[0].firstname = "clark";
24  
25             userarray[1] = new user();
26             userarray[1].lastname = "aaaa";
27             userarray[1].firstname = "bbbbb";
28  
29  
30             ienumerable<data> datalist = createdatalist(userarray, "★★★★★");
31             foreach (data data in datalist)
32             {
33                 console.writeline(data.displayname);
34             }
35             console.writeline("");
36              
37  
38             console.writeline("end");
39             console.readline();
40         }
41  
42         static ienumerable<data> createdatalist(ienumerable<user> userlist, string decorator)
43         {
44             return new castingenumerable<data, user>(userlist, delegate(user user) { return createdata(user, decorator); });
45         }
46  
47         static data createdata(user user, string decorator)
48         {
49             data data = new data();
50             data.displayname = decorator + user.lastname + "." + user.firstname + decorator;
51             return data;
52         }
53     }  
54 }

程式碼 :
view source
 

01 namespace clk.collections
02 {
03     public class castingenumerable<tresult, tsource> : ienumerable<tresult>
04     {
05         //properties 
06         private readonly ienumerable<tsource> _sourceenumerable;
07  
08         private castingenumerabledelegate<tresult, tsource> _enumerabledelegate;
09  
10  
11         // construction 
12         public castingenumerable(ienumerable<tsource> sourceenumerable, castingenumerabledelegate<tresult, tsource> enumerabledelegate)
13         {
14             #region require
15  
16             if (sourceenumerable == null) throw new argumentnullexception();
17             if (enumerabledelegate == null) throw new argumentnullexception();
18  
19             #endregion
20             _sourceenumerable = sourceenumerable;
21             _enumerabledelegate = enumerabledelegate;
22         }
23  
24  
25         // member 
26         public ienumerator<tresult> getenumerator()
27         {
28             return new castingenumerator<tresult, tsource>(_sourceenumerable.getenumerator(), _enumerabledelegate);
29         }
30  
31         system.collections.ienumerator system.collections.ienumerable.getenumerator()
32         {
33             return this.getenumerator();
34         }
35     }
36 }

view source
 

01 namespace clk.collections
02 {
03     public class castingenumerator<tresult, tsource> : ienumerator<tresult>
04     {
05         //properties 
06         private readonly ienumerator<tsource> _sourceenumerator;
07  
08         private readonly castingenumerabledelegate<tresult, tsource> _enumerabledelegate;
09  
10         private tresult _currentresult = default(tresult);
11  
12  
13         // construction 
14         public castingenumerator(ienumerator<tsource> sourceenumerator, castingenumerabledelegate<tresult, tsource> enumerabledelegate)
15         {
16             #region require
17  
18             if (sourceenumerator == null) throw new argumentnullexception();
19             if (enumerabledelegate == null) throw new argumentnullexception();
20  
21             #endregion
22             _sourceenumerator = sourceenumerator;
23             _enumerabledelegate = enumerabledelegate;
24         }
25  
26         public virtual void dispose()
27         {
28             _sourceenumerator.dispose();
29             _currentresult = default(tresult);
30         }
31  
32  
33         // member  
34         public tresult current
35         {
36             get
37             {
38                 return _currentresult;
39             }
40         }
41  
42         object system.collections.ienumerator.current
43         {
44             get
45             {
46                 return this.current;
47             }
48         }
49  
50         public bool movenext()
51         {
52             if (_sourceenumerator.movenext() == true)
53             {
54                 _currentresult = _enumerabledelegate(_sourceenumerator.current);
55                 return true;
56             }
57             return false;
58         }
59  
60         public void reset()
61         {
62             _sourceenumerator.reset();
63             _currentresult = default(tresult);
64         }
65     }
66 }

view source
 

1 public delegate tresult castingenumerabledelegate<tresult, tsource>(tsource source);

 


摘自 昏睡領域