You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.6 KiB

  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace Apewer.Source
  7. {
  8. /// <summary>数据库中的列,类型默认为 NVarChar(191),错误类型将修正为 NText。</summary>
  9. [Serializable]
  10. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  11. public sealed class ColumnAttribute : Attribute
  12. {
  13. private PropertyInfo _property = null;
  14. private string _field = "";
  15. private int _length = 0;
  16. private ColumnType _type;
  17. private bool _independent = false;
  18. private bool _locked = false;
  19. /// <summary>使用自动的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  20. /// <exception cref="System.ArgumentException"></exception>
  21. public ColumnAttribute(ColumnType type = ColumnType.NVarChar, int length = 191)
  22. {
  23. New(null, type, length, true);
  24. }
  25. /// <summary>使用指定的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  26. /// <exception cref="System.ArgumentException"></exception>
  27. public ColumnAttribute(string field, ColumnType type = ColumnType.NVarChar, int length = 191)
  28. {
  29. New(field, type, length, false);
  30. }
  31. internal ColumnAttribute(string field, ColumnType type, int length, bool underline)
  32. {
  33. New(field, type, length, underline);
  34. }
  35. /// <summary>属性。</summary>
  36. public PropertyInfo Property
  37. {
  38. get { return _property; }
  39. internal set
  40. {
  41. if (_locked) return;
  42. _property = value;
  43. }
  44. }
  45. /// <summary>字段名。</summary>
  46. public string Field
  47. {
  48. get { return _field; }
  49. set
  50. {
  51. if (_locked) return;
  52. _field = value;
  53. }
  54. }
  55. /// <summary>指定字段的最大长度。</summary>
  56. public int Length
  57. {
  58. get { return _length; }
  59. set
  60. {
  61. if (_locked) return;
  62. _length = value;
  63. }
  64. }
  65. /// <summary>字段类型。</summary>
  66. public ColumnType Type
  67. {
  68. get { return _type; }
  69. set
  70. {
  71. if (_locked) return;
  72. _type = value;
  73. }
  74. }
  75. /// <summary>Independent 特性。</summary>
  76. public bool Independent
  77. {
  78. get { return _independent; }
  79. internal set { _independent = value; }
  80. }
  81. /// <exception cref="System.ArgumentException"></exception>
  82. private void New(string field, ColumnType type, int length, bool underline)
  83. {
  84. _field = string.IsNullOrEmpty(field) ? "" : TableStructure.RestrictName(field, underline);
  85. _type = type;
  86. switch (type)
  87. {
  88. case ColumnType.VarChar:
  89. case ColumnType.NVarChar:
  90. if (length < 1) throw new ArgumentException("最大长度无效。");
  91. _length = length;
  92. break;
  93. case ColumnType.VarChar255:
  94. case ColumnType.NVarChar255:
  95. _length = 255;
  96. break;
  97. default:
  98. _length = length;
  99. break;
  100. }
  101. Lock();
  102. }
  103. /// <summary>锁定属性,阻止修改。</summary>
  104. public void Lock()
  105. {
  106. _locked = true;
  107. }
  108. }
  109. }