Example #1
0
        private ColumnInfo <T> GetColumnInfoByName <T>(string name, int index)
        {
            var type = typeof(T);

            // First attempt: search by string (ignore case).
            var pi = type.GetProperty(name, MapHelper.BindingFlag);

            if (pi == null)
            {
                // Second attempt: search display name of DisplayAttribute if any.
                foreach (var propertyInfo in type.GetProperties(MapHelper.BindingFlag))
                {
                    var atts = propertyInfo.GetCustomAttributes <DisplayAttribute>();

                    if (atts.Any(att => string.Equals(@att.Name, @name, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        pi = propertyInfo;
                        break;
                    }
                }
            }

            if (pi == null)
            {
                // Third attempt: remove ignored chars and do the truncation.
                pi = type.GetProperty(MapHelper.GetRefinedName(name, IgnoredNameChars, TruncateNameFrom), MapHelper.BindingFlag);
            }

            if (pi == null)
            {
                return(null);
            }

            ColumnAttribute attribute = null;

            if (Attributes.ContainsKey(pi))
            {
                attribute = Attributes[pi].Clone(index);
                if (attribute.Ignored == true)
                {
                    return(null);
                }
            }

            return(attribute == null ? new ColumnInfo <T>(name, index, pi) : new ColumnInfo <T>(name, attribute));
        }