Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7.  
  8.  
  9. namespace ConsoleApplication6
  10. {
  11. class Employee
  12. {
  13. public string Name { get; set; }
  14. public string Surname { get; set; }
  15. public decimal Salary { get; set; }
  16. public DateTime bornDate { get; set; }
  17. public double Weight { get; set; }
  18. public bool IsAvailable { get; set; }
  19. public Employee()
  20. {
  21.  
  22. }
  23. public Employee(object T)
  24. {
  25. // Wszędzie gdzie jest ten obiekt e to przygotowanie do zwracania w innej metodzie niż konstruktor
  26. //Employee e = new Employee();
  27. Dictionary<string, object> dc = new Dictionary<string, object>();
  28. foreach (PropertyInfo prop in T.GetType().GetProperties())
  29. {
  30. dc.Add(prop.Name, prop.GetValue(T));
  31. }
  32. /*
  33. foreach (PropertyInfo prop in e.GetType().GetRuntimeProperties())
  34. {
  35. dc[prop.Name]
  36. }
  37. */
  38. if (dc.ContainsKey("Name"))
  39. {
  40. //e.Name = (string)dc["Name"];
  41. this.Name = (string)dc["Name"];
  42. }
  43. if (dc.ContainsKey("Surname"))
  44. {
  45. //e.Surname = (string)dc["Surname"];
  46. this.Surname = (string)dc["Surname"];
  47. }
  48. // jest problem z rzutowaniem
  49. /*
  50. if (dc.ContainsKey("Salary"))
  51. {
  52. //e.Salary = (decimal)dc["Salary"];
  53. this.Salary = (decimal)dc["Salary"];
  54. }
  55. */
  56. if (dc.ContainsKey("bornDate"))
  57. {
  58. // e.bornDate = (DateTime)dc["bornDate"];
  59. this.bornDate = (DateTime)dc["bornDate"];
  60. }
  61. if (dc.ContainsKey("Weight"))
  62. {
  63. //e.Weight = (double)dc["Weight"];
  64. this.Weight = (double)dc["Weight"];
  65. }
  66. if (dc.ContainsKey("IsAvailable"))
  67. {
  68. //e.IsAvailable = (bool)dc["IsAvailable"];
  69. this.IsAvailable = (bool)dc["IsAvailable"];
  70. }
  71. //return e
  72. }
  73. }
  74.  
  75. class Program
  76. {
  77. static void Main(string[] args)
  78. {
  79. var cos = new { Surname = "Kowalski", Salary = 2110, IsAvailable = false };
  80. Employee emp = new Employee(cos);
  81. Console.WriteLine(emp.Surname);
  82. Console.WriteLine(emp.Salary);
  83. Console.WriteLine(emp.IsAvailable);
  84. Console.ReadLine();
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement