Metadata were not loaded using MetadataType

Solution 1:

because metadatatype is not for such porposes but you can use this method

private bool PropertyHasAttribute<T>(string properyName, Type attributeType)
    {
        MetadataTypeAttribute att = (MetadataTypeAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(MetadataTypeAttribute));
        if (att != null)
        {
            ;
            foreach (var prop in Type.GetType(att.MetadataClassType.UnderlyingSystemType.FullName).GetProperties())
            {
                if (properyName.ToLower() == prop.Name.ToLower() && Attribute.IsDefined(prop,attributeType))
                    return true;
            }
        }
        return false;
    }

and you can use like this

bool res = PropertyHasAttribute<ClientInfoView>("Login", typeof(DisplayAttribute))

this tell you that the class property login has or has not displayattribute , but if you need to findout attribute Value you can use Attribute.GetCustomAttribute method and cast it to your selected attribute like display attribute and read Name property by .Name :)

Solution 2:

To give some part of the answer , you can check ClientInfoView has got attributes. Some small demo that worked for me. Still trying to find why I can't access those attributes in ClientInfoViewMetaData individual properties

    static void Main(string[] args)
    {
        TypeDescriptor.AddProviderTransparent(
        new AssociatedMetadataTypeTypeDescriptionProvider(typeof(ClientInfoView), typeof(ClientInfoViewMetaData)), typeof(ClientInfoView));
        ClientInfoView cv1 = new ClientInfoView() { ID = 1 };
        var df = cv1.GetType().GetCustomAttributes(true);
        var dfd = cv1.ID.GetType().GetCustomAttributes(typeof(DisplayNameAttribute), true);
        var context = new ValidationContext(cv1, null, null);
        var results = new List<ValidationResult>();
        var isValid = Validator.TryValidateObject( cv1,context, results, true);
    }
}

    [MetadataType(typeof(ClientInfoViewMetaData))]
    public partial class ClientInfoView
    {
        public int ID { get; set; }
        public string Login { get; set; }
    }

public class ClientInfoViewMetaData
{        
    [Required]
    [Category("Main Data"), DisplayName("Client ID")]
    public int ID { get; set; }

    [Required]
    [Category("Main Data"), DisplayName("Login")]
    public string Login { get; set; }

}