泛型索引器类代码
最近在做一个管理器类,老总要求里面的Items属性可以用索引器访问,像Dictionary<T>一样;但又要求只读不改。代码刚刚成型,如有好的建议请提出。
使用设计管理器时的代码:
管理器的属性定义为
1 /// <summary>
2 /// 泛型索引器
3 /// </summary>
4 /// <typeparam name="T">泛型</typeparam>
5 /// <seealso cref="System.Collections.Generic.IEnumerable<T>"/>
6 public class Indexer<T> : IEnumerable<T>
7 {
8 /// <summary>
9 /// 指向要用到
10 /// </summary>
11 private Dictionary<string, T> dict;
12
13 /// <summary>
14 /// 遇到错误时报错
15 /// </summary>
16 private bool allowThrowReadError = false;
17
18 /// <summary>
19 /// 构造方法
20 /// </summary>
21 /// <param name="dict"></param>
22 /// <param name="allowThrowReadError">遇到错误时报错</param>
23 public Indexer(Dictionary<string, T> dict,bool allowThrowReadError)
24 {
25 this.dict = new Dictionary<string, T>(dict);
26 }
27
28 /// <summary>
29 /// 索引器
30 /// </summary>
31 /// <param name="key">键值</param>
32 /// <returns>返回值</returns>
33 /// <typeparam name="T">泛型</typeparam>
34 /// <exception cref="Exception">当allowThrowReadError=true并且Key不存在时报异常</exception>
35 public T this[String key]
36 {
37 get
38 {
39 if (dict == null) throw new Exception("索引器失效");
40 if (dict.ContainsKey(key))
41 {
42 return dict[key];
43 }
44 if (allowThrowReadError) throw new Exception("不存在键值");
45 else return default(T);
46 }
47 }
48
49 /// <summary>
50 /// 数目
51 /// </summary>
52 public int Count
53 {
54 get { return dict.Count; }
55 }
56
57 IEnumerator<T> IEnumerable<T>.GetEnumerator()
58 {
59 return dict.Values.GetEnumerator();
60 }
61
62 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
63 {
64 return dict.Values.GetEnumerator();
65 }
66
2 /// 泛型索引器
3 /// </summary>
4 /// <typeparam name="T">泛型</typeparam>
5 /// <seealso cref="System.Collections.Generic.IEnumerable<T>"/>
6 public class Indexer<T> : IEnumerable<T>
7 {
8 /// <summary>
9 /// 指向要用到
10 /// </summary>
11 private Dictionary<string, T> dict;
12
13 /// <summary>
14 /// 遇到错误时报错
15 /// </summary>
16 private bool allowThrowReadError = false;
17
18 /// <summary>
19 /// 构造方法
20 /// </summary>
21 /// <param name="dict"></param>
22 /// <param name="allowThrowReadError">遇到错误时报错</param>
23 public Indexer(Dictionary<string, T> dict,bool allowThrowReadError)
24 {
25 this.dict = new Dictionary<string, T>(dict);
26 }
27
28 /// <summary>
29 /// 索引器
30 /// </summary>
31 /// <param name="key">键值</param>
32 /// <returns>返回值</returns>
33 /// <typeparam name="T">泛型</typeparam>
34 /// <exception cref="Exception">当allowThrowReadError=true并且Key不存在时报异常</exception>
35 public T this[String key]
36 {
37 get
38 {
39 if (dict == null) throw new Exception("索引器失效");
40 if (dict.ContainsKey(key))
41 {
42 return dict[key];
43 }
44 if (allowThrowReadError) throw new Exception("不存在键值");
45 else return default(T);
46 }
47 }
48
49 /// <summary>
50 /// 数目
51 /// </summary>
52 public int Count
53 {
54 get { return dict.Count; }
55 }
56
57 IEnumerator<T> IEnumerable<T>.GetEnumerator()
58 {
59 return dict.Values.GetEnumerator();
60 }
61
62 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
63 {
64 return dict.Values.GetEnumerator();
65 }
66
使用设计管理器时的代码:
管理器的属性定义为