ASP.NET:KeyValuePair之自定义用法

今天自己写了一个KeyValue类,主要是封装KeyValuePair,简化KeyValuePair的初始化和赋值方法,用起来更简单些:)

 1 public class KeyValue

 2 {

 3     /// <summary>

 4     /// 内部键值对象

 5     /// </summary>

 6     private KeyValuePair<object, object> obj = new KeyValuePair<object, object>();

 7 

 8     /// <summary>

 9     /// 返回数据

10     /// </summary>

11     public Object Data { get; set; }

12 

13     /// <summary>

14     ///

15     /// </summary>

16     public Object Key

17     {

18         get { return obj.Key; }

19         set { obj = new KeyValuePair<object, object>(value, obj.Value); }

20     }

21 

22     /// <summary>

23     ///

24     /// </summary>

25     public Object Value

26     {

27         get { return obj.Value; }

28         set { obj = new KeyValuePair<object, object>(obj.Key, value); }

29     }

30 

31     /// <summary>

32     /// 设置键值

33     /// </summary>

34     /// <param name="key"></param>

35     /// <param name="value"></param>

36     public void SetKeyValue(Object key, Object value) { obj = new KeyValuePair<object, object>(key, value); }

37     public void SetKeyValue(Object key, Object value, Object data)

38     {

39         obj = new KeyValuePair<object, object>(key, value);

40         this.Data = data;

41     }

42 

43     /// <summary>

44     /// 初始化键值对象

45     /// </summary>

46     /// <param name="key"></param>

47     /// <param name="value"></param>

48     public KeyValue(object key, object value) { SetKeyValue(key, value); }

49     public KeyValue(object key, object value, object data) { SetKeyValue(key, value, data); }

50 

51 }

用法示例:

  KeyValue kv = new KeyValue("Hello", "World");

  KeyValue kv1 = new KeyValue("Hello", "World", kv);

  kv1.Key = "Hello World Key!";

  kv1.Value = "Hello World Value!";

  kv1.Data = kv;

  kv1.SetKeyValue("Hello", "World");

  kv1.SetKeyValue("Hello", "World", kv);

结合List,用起来简捷方便,易于扩展,以此为作键值基础,下一个示例将使用这个KeyValue来简化代码。

你可能感兴趣的:(asp.net)