Retrieve display names from Validator

Anas TINA used Ask the Experts™
Hi Experts,

I am running on dot net 4.6.1

I have the same problem described in the following question

Please help ASAP,

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Hi Anas,

Please post the actual question. This way people will be able to help you easily.

Are you wanting something like this:
using EE_Q29145830.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace EE_Q29145830
    class Program
        static void Main(string[] args)
            var user = new User();
            var errors = user.GetValidationErrors();
            foreach (var error in errors)

    class User
        [Display(Name = "LOGIN_FIELD", ResourceType = typeof(Resources))]
        [Required(ErrorMessageResourceName = "MISSING_LOGIN", ErrorMessageResourceType = typeof(Resources))]
        [StringLength(20, MinimumLength = 3, ErrorMessageResourceName = "LOGIN_LENGTH_ERROR", ErrorMessageResourceType = typeof(Resources))]
        [RegularExpression("^(?i)[a-z0-9]{1,40}$", ErrorMessageResourceName = "LOGIN_FORMAT_ERROR", ErrorMessageResourceType = typeof(Resources))]
        public string LOGIN { get; set; }

    static class Extensions
        public static IEnumerable<dynamic> GetValidationErrors<T>(this T source)
            var context = new ValidationContext(source);
            var results = new List<ValidationResult>();
            Validator.TryValidateObject(source, context, results, true);

            return (from result in results
                    from member in result.MemberNames
                    let property = typeof(T).GetProperty(member)
                    where !Equals(property, null)
                    from attribute in property.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>()
                    select new { MemberName = member, result.ErrorMessage, DisplayName = attribute.Name });

Open in new window

Which produces the following output -Capture.PNG-saige-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial