CSVDataObject.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class CSVDataObject : IEnumerable
  6. {
  7. /// <summary>
  8. /// 此值作为数据对象的唯一标识,只能通过此属性获取到唯一标识
  9. /// 无法通过 '数据对象[主键名]' 的方式来获取
  10. /// </summary>
  11. public string ID { get { return _major; } }
  12. private readonly string _major;
  13. /// <summary>
  14. /// 一条数据应包含的所有的键名
  15. /// </summary>
  16. public string[] AllKeys { get { return _allKeys; } }
  17. private readonly string[] _allKeys;
  18. private Dictionary<string, string> _atrributesDic;
  19. /// <summary>
  20. /// 初始化,获取唯一标识与除主键之外所有属性的键与值
  21. /// </summary>
  22. /// <param name="major"> 唯一标识,主键 </param>
  23. /// <param name="atrributeDic"> 除主键值外的所有属性键值字典 </param>
  24. public CSVDataObject(string major, Dictionary<string, string> atrributeDic, string[] allKeys)
  25. {
  26. _major = major;
  27. _atrributesDic = atrributeDic;
  28. _allKeys = allKeys;
  29. }
  30. /// <summary>
  31. /// 获取数据对象的签名,用于比较是否与数据表的签名一致
  32. /// </summary>
  33. /// <returns> 数据对象的签名 </returns>
  34. public string GetFormat()
  35. {
  36. string format = string.Empty;
  37. foreach (string key in _allKeys)
  38. {
  39. format += (key + "-");
  40. }
  41. return format;
  42. }
  43. public string this[string key]
  44. {
  45. get { return GetValue(key); }
  46. set { SetKey(key, value); }
  47. }
  48. private void SetKey(string key, string value)
  49. {
  50. if (_atrributesDic.ContainsKey(key))
  51. _atrributesDic[key] = value;
  52. else
  53. Debug.LogError("The data not include the key.");
  54. }
  55. private string GetValue(string key)
  56. {
  57. string value = string.Empty;
  58. if (_atrributesDic.ContainsKey(key))
  59. value = _atrributesDic[key];
  60. else
  61. Debug.LogError("The data not include value of this key.");
  62. return value;
  63. }
  64. public override string ToString()
  65. {
  66. string content = string.Empty;
  67. if (_atrributesDic != null)
  68. {
  69. foreach (var item in _atrributesDic)
  70. {
  71. content += (item.Key + ": " + item.Value + ". ");
  72. }
  73. }
  74. return content;
  75. }
  76. public IEnumerator GetEnumerator()
  77. {
  78. foreach (var item in _atrributesDic)
  79. {
  80. yield return item;
  81. }
  82. }
  83. }