using Apewer.Internals; using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace Apewer.Source { /// 数据库中的列,类型默认为 NVarChar(191),错误类型将修正为 NText。 [Serializable] [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public sealed class ColumnAttribute : Attribute { private PropertyInfo _property = null; private string _field = ""; private int _length = 0; private ColumnType _type; private bool _independent = false; /// private void Init(string field, ColumnType type, int length, bool underline) { _field = string.IsNullOrEmpty(field) ? "" : TableStructure.RestrictName(field, underline); _type = type; switch (type) { case ColumnType.VarChar: case ColumnType.NVarChar: if (length < 1) throw new ArgumentException("最大长度无效。"); _length = length; break; case ColumnType.VarChar255: case ColumnType.NVarChar255: _length = 255; break; default: _length = length; break; } } /// 使用自动的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。 /// public ColumnAttribute(ColumnType type = ColumnType.NVarChar, int length = 191) => Init(null, type, length, true); /// 使用指定的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。 /// public ColumnAttribute(string field, ColumnType type = ColumnType.NVarChar, int length = 191) => Init(field, type, length, false); internal ColumnAttribute(string field, ColumnType type, int length, bool underline) => Init(field, type, length, underline); /// 属性。 public PropertyInfo Property { get => _property; internal set => _property = value; } /// 字段名。 public string Field { get => _field; set => _field = value; } /// 指定字段的最大长度。 public int Length { get => _length; set => _length = value; } /// 字段类型。 public ColumnType Type { get => _type; set => _type = value; } /// Independent 特性。 public bool Independent { get => _independent; internal set => _independent = value; } } }