• Source
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4.  
    5. public class Employee
    6. {
    7. public int Id {get; set;}
    8. public string FirstName { get; set;}
    9. public string LastName {get; set;}
    10. }
    11.  
    12. public class Test
    13. {
    14. private static string[] GetColumnValues(Employee emp, params int[] cols)
    15. {
    16. var props = emp.GetType().GetProperties();
    17. var values = new List<string>();
    18. foreach(var i in cols)
    19. {
    20. if (i >= 0 && i < props.Length)
    21. {
    22. object value = props[i].GetValue(emp, null);
    23. values.Add(value == null ? string.Empty : value.ToString());
    24. }
    25. }
    26. return values.ToArray();
    27. }
    28. public static void Main()
    29. {
    30. var emp = new Employee() { Id = 1, FirstName = "John", LastName = "Smith" };
    31. var values = GetColumnValues(emp, 0, 2);
    32. Console.WriteLine(string.Join("\t", values));
    33. }
    34. }