mercredi 13 février 2019

Looping through each class property and getting it's value using Reflection

I have created a public class, Applicant. I have created a new Applicant object on my code and I want to loop through each of the properties of the applicant class. I am using the code below, however I am getting the error: "Object does not match target type." Thanks for your help

public class Applicant
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string JobTitle { get; set; }
    public string Company { get; set; }
    public string Email { get; set; }
    public string TelephoneNumber { get; set; }
    public string LinkedIn { get; set; }
    public string Website { get; set; }
    public string Instagram { get; set; }

    public Applicant(string _firstname, string _lastname, string _jobtitle, string _company, string _email,
        string _telephonenumber, string _linkedin, string _website, string _instagram)
    {
        Firstname = _firstname;
        Lastname = _lastname;
        JobTitle = _jobtitle;
        Company = _company;
        Email = _email;
        TelephoneNumber = _telephonenumber;
        LinkedIn = _linkedin;
        Website = _website;
        Instagram = _instagram;
    }
}
    //Create a new Applicant object
    Applicant applicant = new Applicant(Firstname.Value, Lastname.Value,
    JobTitle.Value, Businessname.Value, Email.Value, TelephoneNumber.Value, Linkedin.Value, Companywebsite.Value, Instagram.Value);

    PropertyInfo[] properties = applicant.GetType().GetProperties();
    foreach (var p in properties)
    {
    Response.Write(p.GetValue(properties, null).ToString());
    }





Aucun commentaire:

Enregistrer un commentaire