Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 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. if (dc["Name"].GetType() == typeof(string))
  41. {
  42. //e.Name = (string)dc["Name"];
  43. this.Name = (string)dc["Name"];
  44. }
  45.  
  46. }
  47. if (dc.ContainsKey("Surname"))
  48. {
  49. if (dc["Surname"].GetType() == typeof(string))
  50. {
  51. //e.Surname = (string)dc["Surname"];
  52. this.Surname = (string)dc["Surname"];
  53. }
  54. }
  55. // jest problem z rzutowaniem
  56.  
  57. if (dc.ContainsKey("Salary"))
  58. {
  59. if (dc["Salary"].GetType() == typeof(decimal))
  60. {
  61. //e.Salary = (decimal)dc["Salary"];
  62. this.Salary = (decimal)dc["Salary"];
  63. }
  64. }
  65.  
  66. if (dc.ContainsKey("bornDate"))
  67. {
  68. if (dc["bornDate"].GetType() == typeof(DateTime))
  69. {
  70. // e.bornDate = (DateTime)dc["bornDate"];
  71.  
  72. this.bornDate = (DateTime)dc["bornDate"];
  73. }
  74. }
  75. if (dc.ContainsKey("Weight"))
  76. {
  77. if (dc["Weight"].GetType() == typeof(double))
  78. {
  79. //e.Weight = (double)dc["Weight"];
  80. this.Weight = (double)dc["Weight"];
  81. }
  82. }
  83. if (dc.ContainsKey("IsAvailable"))
  84. {
  85. if (dc["IsAvailable"].GetType() == typeof(bool))
  86. {
  87. //e.IsAvailable = (bool)dc["IsAvailable"];
  88. this.IsAvailable = (bool)dc["IsAvailable"];
  89. }
  90.  
  91. }
  92. //return e
  93. }
  94. }
  95.  
  96. class Program
  97. {
  98. static void Main(string[] args)
  99. {
  100. var cos = new {Name = "Jan", Surname = "Kowalski", Salary = 2000, IsAvailable = "dsadas", Weight = 20.9d };
  101. Employee emp = new Employee(cos);
  102. Console.WriteLine(cos.Name.GetType());
  103. Console.WriteLine(emp.Name.GetType());
  104. Console.WriteLine(emp.Name);
  105. Console.WriteLine(emp.Surname);
  106. Console.WriteLine(emp.Salary);
  107. Console.WriteLine(emp.IsAvailable);
  108. Console.WriteLine(emp.Weight);
  109. Console.ReadLine();
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement