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.
|
|
using System;using System.Collections.Generic;using System.Reflection;
namespace Apewer.Source{
/// <summary>索引。</summary>
[Serializable] [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)] public sealed class IndexAttribute : Attribute, IToJson {
PropertyInfo _property = null; ColumnAttribute _column;
string _name = null; FieldOrder _order = FieldOrder.Ascend;
/// <summary>索引名称。</summary>
public string Name { get => _name; }
/// <summary>排序。</summary>
public FieldOrder Order { get => _order; set => _order = value; }
/// <summary>列。</summary>
public ColumnAttribute Column { get => _column; }
/// <summary>属性。</summary>
public PropertyInfo Property { get => _property; }
/// <summary>表示此属性是索引的一部分。</summary>
/// <param name="name">索引名称。</param>
public IndexAttribute(string name) { if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name)); _name = name; }
internal void SetColumn(ColumnAttribute column) { _column = column; }
/// <summary>解析列的索引。</summary>
/// <remarks>注意:此方法不再抛出异常,当不存在正确的列特性时将返回 NULL 值</remarks>
public static IndexAttribute[] Parse(PropertyInfo property) { if (property == null) return null;
var attributes = property.GetCustomAttributes(typeof(IndexAttribute), true); var ias = new List<IndexAttribute>(); foreach (var attribute in attributes) { var ia = attribute as IndexAttribute; if (ia == null) continue; if (ia._name.IsEmpty()) continue;
ia._property = property; ias.Add(ia); } return ias.ToArray(); }
/// <summary>生成 Json 对象。</summary>
/// <exception cref="NotImplementedException"></exception>
public Json ToJson() { var json = Json.NewObject(); json.SetProperty("name", _name); json.SetProperty("order", _order.ToString()); if (_column != null) json.SetProperty("column", _column.ToJson()); return json; }
/// <summary></summary>
public override string ToString() => $"Index = {_name}, Field = {_column?.Field}, Order = {_order}";
}
}
|