Exemplo n.º 1
0
        /// <summary>
        ///     创建实体属性元数据。
        /// </summary>
        /// <param name="property"></param>
        /// <param name="attributes"></param>
        /// <returns></returns>
        protected virtual IEntityPropertyMetadata CreateEntityPropertyMetadata(PropertyInfo property,
                                                                               IEnumerable <Attribute> attributes)
        {
            var result = new EntityPropertyMetadata(property)
            {
                IsSearcher = attributes.Any(x => x is SearcherAttribute),
                IsRequired = attributes.Any(x => x is RequiredAttribute),
                IsKey      = attributes.Any(x => x is KeyAttribute),
                DataType   = EntityExtensions.GetDataType(property)
            };

            var    display = attributes.OfType <DisplayAttribute>().FirstOrDefault();
            string name    = null;

            if (display != null)
            {
                result.Description = display.GetDescription();
                result.Order       = display.GetOrder() ?? ModelMetadata.DefaultOrder;
                result.GroupName   = display.GetGroupName();
                name = display.GetName();
            }

            if (name != null)
            {
                result.Name = name;
            }
            else
            {
                var displayNameAttribute = attributes.OfType <DisplayNameAttribute>().FirstOrDefault();
                if (displayNameAttribute != null)
                {
                    result.Name = displayNameAttribute.DisplayName;
                }
            }
            if (string.IsNullOrEmpty(result.Name))
            {
                result.Name = property.Name;
            }

            return(result);
        }