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.

82 lines
2.9 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(255),错误类型将修正为 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. /// <summary>使用自动的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  19. /// <exception cref="System.ArgumentException"></exception>
  20. public ColumnAttribute(ColumnType type = ColumnType.NVarChar255, int length = 0)
  21. {
  22. New(null, type, length, true);
  23. }
  24. /// <summary>使用指定的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  25. /// <exception cref="System.ArgumentException"></exception>
  26. public ColumnAttribute(string field, ColumnType type = ColumnType.NVarChar255, int length = 0)
  27. {
  28. New(field, type, length, false);
  29. }
  30. internal ColumnAttribute(string field, ColumnType type, int length, bool underline)
  31. {
  32. New(field, type, length, underline);
  33. }
  34. /// <summary>属性。</summary>
  35. public PropertyInfo Property { get { return _property; } internal set { _property = value; } }
  36. /// <summary>字段名。</summary>
  37. public string Field { get { return _field; } set { _field = value; } }
  38. /// <summary>指定字段的最大长度。</summary>
  39. public int Length { get { return _length; } set { _length = value; } }
  40. /// <summary>字段类型。</summary>
  41. public ColumnType Type { get { return _type; } set { _type = value; } }
  42. /// <summary>Independent 特性。</summary>
  43. public bool Independent { get { return _independent; } internal set { _independent = value; } }
  44. /// <exception cref="System.ArgumentException"></exception>
  45. private void New(string field, ColumnType type, int length, bool underline)
  46. {
  47. _field = string.IsNullOrEmpty(field) ? "" : TableStructure.RestrictName(field, underline);
  48. _type = type;
  49. switch (type)
  50. {
  51. case ColumnType.VarChar:
  52. case ColumnType.NVarChar:
  53. if (length < 1) throw new ArgumentException("最大长度无效。");
  54. _length = length;
  55. break;
  56. case ColumnType.VarChar255:
  57. case ColumnType.NVarChar255:
  58. _length = 255;
  59. break;
  60. default:
  61. _length = length;
  62. break;
  63. }
  64. }
  65. }
  66. }