none
Reflection List Property name and value RRS feed

  • Question

  • https://stackoverflow.com/questions/5508050/how-to-get-a-property-value-based-on-the-name/5508090#5508090

    Hi,

    I'm writing a custom export to excel function using c#

    I have a entity (EmployeeDetaisl) with has several properties in it

    For e.g.
    FirstName
    LastName
    Department
    Age

    I want to write a function to loop thorough all the property and get the propertyname and value so that I can pass it to my export to excel function which will intern write in on specific cell( I will be passing these cell locations as parameter)

    Thanks

    S

    Thursday, October 4, 2018 11:09 AM

Answers

  • Hi, try this way:

    public class EmployeeDetails
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }
        public int Age { get; set; }
    }
    
    public static void Main(string[] args)
    {
        var employeeDetails = new EmployeeDetails()
        {
            FirstName = "aaaaa",
            LastName = "bbbbb",
            Department = "ccccc",
            Age = 29
        };
    
    
        foreach (var property in  typeof(EmployeeDetails).GetProperties())
        {
            Console.WriteLine($"{property.Name}: {property.GetValue(employeeDetails,null)}");
        }
    }
    

    • Marked as answer by StSingh Thursday, October 4, 2018 11:43 AM
    Thursday, October 4, 2018 11:25 AM

All replies

  • Hello, 

    You already have your Entity.

    To get a list of the field - add simple method returned required list.

    If you insist on using Reflection - 

    Type t = typeof(EmployeeDetaisl);
    PropertyInfo[] pi = t.GetProperties(<Put here a Flag's matchet your properties>);


    Sincerely, Highly skilled coding monkey.

    Thursday, October 4, 2018 11:22 AM
  • Hi, try this way:

    public class EmployeeDetails
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Department { get; set; }
        public int Age { get; set; }
    }
    
    public static void Main(string[] args)
    {
        var employeeDetails = new EmployeeDetails()
        {
            FirstName = "aaaaa",
            LastName = "bbbbb",
            Department = "ccccc",
            Age = 29
        };
    
    
        foreach (var property in  typeof(EmployeeDetails).GetProperties())
        {
            Console.WriteLine($"{property.Name}: {property.GetValue(employeeDetails,null)}");
        }
    }
    

    • Marked as answer by StSingh Thursday, October 4, 2018 11:43 AM
    Thursday, October 4, 2018 11:25 AM
  • Thanks

    @Krzysztof

    Why should I pass the entire entity to get the value of property... si there something like passing index or property name?

     Console.WriteLine($"{property.Name}: {property.GetValue(employeeDetails,null)}");

    Thursday, October 4, 2018 11:41 AM
  • Thanks

    @Krzysztof

    Why should I pass the entire entity to get the value of property... si there something like passing index or property name?

     Console.WriteLine($"{property.Name}: {property.GetValue(employeeDetails,null)}");

    Understood... tube-light :)
    Thursday, October 4, 2018 11:44 AM