Guest User

Untitled

a guest
Feb 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. # All Class Object Properties as a String
  2.  
  3. #### Simple
  4. ```CSharp
  5. Func<MyClass, string> MyFunc =
  6. (a) =>
  7. a
  8. .GetType()
  9. .GetProperties()
  10. .Select(p => p
  11. .GetValue(a));
  12. ```
  13.  
  14. #### For CSV
  15. ```CSharp
  16. Func<MyClass, string> MyFunc =
  17. (a) => "\"" +
  18. string.Join("\",\"",
  19. a
  20. .GetType()
  21. .GetProperties()
  22. .Select(p => p
  23. .GetValue(a)))
  24. + "\"";
  25. ```
  26.  
  27. ---
  28.  
  29. ## Example
  30.  
  31. ```CSharp
  32. public class User
  33. {
  34. public int Id { get; set; }
  35. public string FirstInitial { get; set; }
  36. public string LastName { get; set; }
  37. public bool IsStudent { get; set; }
  38. public string PhoneNumber { get; set; }
  39. public string Ethnicity { get; set; }
  40. ...
  41. }
  42. ```
  43.  
  44. ```CSharp
  45. Func<User, string> GetAllProperties = (a) => "\"" + string.Join("\",\"", aGetType().GetProperties().Select(p => p.GetValue(a)))+ "\"";
  46. ...
  47. using(StreamWriter outFile = new StreamWriter(fp))
  48. {
  49. userList.ForEach(user => outFile.WriteLine(GetAllProperties(user)));
  50. }
  51. ```
  52.  
  53. ### Output file
  54.  
  55. ```txt
  56. "1159","J","English","false","",...,""
  57. ```
Add Comment
Please, Sign In to add comment