andrew4582

KeyItem and KeyItemCollection

Nov 20th, 2012
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.ComponentModel;
  7.  
  8. namespace Core
  9. {
  10.     /// <summary>
  11.     /// Used to hold various key-value data
  12.     /// </summary>
  13.     [Serializable]
  14.     [DebuggerDisplay("Key = {Key}, Value = {Value}")]
  15.     public class KeyItem
  16.     {
  17.         string _key;
  18.         string _value;
  19.  
  20.         public string Key
  21.         {
  22.             get { return _key; }
  23.             set { _key = value; }
  24.         }
  25.  
  26.         public string Value
  27.         {
  28.             get { return _value; }
  29.             set { _value = value; }
  30.         }
  31.  
  32.         public KeyItem() { }
  33.         public KeyItem(string k, string v)
  34.         {
  35.             _key = k;
  36.             _value = v;
  37.         }
  38.     }
  39.        /// <summary>
  40.     /// Collection of <seealso cref="KeyItem"/> objects that holds basic key-value data
  41.     /// </summary>
  42.     [Serializable]
  43.     [DebuggerDisplay("Count = {Count}")]
  44.     public class KeyItemCollection : List<KeyItem>
  45.     {
  46.         public bool TryGetValue(string key, out KeyItem item)
  47.         {
  48.             item = this.FirstOrDefault(t => t.Key == key);
  49.             return item != null;
  50.         }
  51.  
  52.         #region Indexer
  53.         public string this[string key]
  54.         {
  55.             get
  56.             {
  57.                 KeyItem kitem = null;
  58.                 if (this.TryGetValue(key, out kitem))
  59.                     return kitem.Value;
  60.                 return null;
  61.             }
  62.             set
  63.             {
  64.                 KeyItem kitem = null;
  65.  
  66.                 if (this.TryGetValue(key, out kitem))
  67.                 {
  68.                     kitem.Value = value;
  69.                 }
  70.                 else
  71.                 {
  72.                     this.Add(new KeyItem(key, value));
  73.                 }
  74.             }
  75.         }
  76.         #endregion
  77.  
  78.         #region ValueAs methods
  79.  
  80.         public string ValueAsString(string key, string valueIfNull = default(string))
  81.         {
  82.             KeyItem kitem = null;
  83.  
  84.             if (!this.TryGetValue(key, out kitem))
  85.                 return valueIfNull;
  86.  
  87.             if (string.IsNullOrEmpty(kitem.Value))
  88.             {
  89.                 return valueIfNull;
  90.             }
  91.             else
  92.                 return kitem.Value;
  93.         }
  94.  
  95.         public int ValueAsInt(string key, int valueIfNull = default(int))
  96.         {
  97.             return ValueAs<int>(key, valueIfNull);
  98.         }
  99.  
  100.         public T ValueAs<T>(string key, T valueIfNull = default(T))
  101.         {
  102.             KeyItem kitem = null;
  103.             Type valType = typeof(T);
  104.  
  105.             if (!this.TryGetValue(key, out kitem))
  106.                 return valueIfNull;
  107.  
  108.             if (kitem == null)
  109.             {
  110.                 return valueIfNull;
  111.             }
  112.  
  113.             if (valType.IsGenericType && valType.GetGenericTypeDefinition() == typeof(Nullable<>))
  114.             {
  115.                 if (string.IsNullOrEmpty(kitem.Value))
  116.                 {
  117.                     return valueIfNull;
  118.                 }
  119.                 else
  120.                 {
  121.                     TypeConverter conv = TypeDescriptor.GetConverter(typeof(T));
  122.                     return (T)conv.ConvertFrom(kitem.Value);
  123.                 }
  124.             }
  125.             else
  126.             {
  127.                 if (string.IsNullOrEmpty(kitem.Value))
  128.                 {
  129.                     return valueIfNull;
  130.                 }
  131.             }
  132.             return (T)Convert.ChangeType(kitem.Value, typeof(T));
  133.         }
  134.         #endregion
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment