internal IMemberAccessor GetDisplayMember(Type entityType)
        {
            return(_displayCache.GetOrAdd(entityType, key =>
            {
                TypeAccessor typeAccessor = TypeAccessor.GetAccessor(entityType);
                IMemberAccessor displayMember = null;

                var displayAttribute = GetAttribute <DisplayColumnAttribute>(entityType);

                // first try DisplayColumnAttribute property
                if (displayAttribute != null)
                {
                    displayMember = typeAccessor.FindProperty(displayAttribute.DisplayColumn);
                }

                if (displayMember != null)
                {
                    return displayMember;
                }

                var properties = typeAccessor.GetProperties().ToList();

                // try first string property
                displayMember = properties.FirstOrDefault(m => m.MemberType == typeof(string));
                if (displayMember != null)
                {
                    return displayMember;
                }

                // try second property
                return properties.Count > 1 ? properties[1] : null;
            }));
        }
Example #2
0
        /// <summary>
        /// Searches for the property using a property expression.
        /// </summary>
        /// <typeparam name="T">The object type containing the property specified in the expression.</typeparam>
        /// <param name="propertyExpression">The property expression (e.g. p => p.PropertyName)</param>
        /// <returns>An <see cref="IMemberAccessor"/> instance for the property if found; otherwise <c>null</c>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="propertyExpression"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown when the expression is:<br/>
        ///     Not a <see cref="MemberExpression"/><br/>
        ///     The <see cref="MemberExpression"/> does not represent a property.<br/>
        ///     Or, the property is static.
        /// </exception>
        public static IMemberAccessor FindProperty <T>(Expression <Func <T> > propertyExpression)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }

            TypeAccessor typeAccessor = TypeAccessor.GetAccessor(typeof(T));

            return(typeAccessor.FindProperty <T>(propertyExpression));
        }