Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- namespace ConsoleApplication6
- {
- class Employee
- {
- public string Name { get; set; }
- public string Surname { get; set; }
- public decimal Salary { get; set; }
- public DateTime bornDate { get; set; }
- public double Weight { get; set; }
- public bool IsAvailable { get; set; }
- public Employee()
- {
- }
- public Employee(object T)
- {
- // Wszędzie gdzie jest ten obiekt e to przygotowanie do zwracania w innej metodzie niż konstruktor
- //Employee e = new Employee();
- Dictionary<string, object> dc = new Dictionary<string, object>();
- foreach (PropertyInfo prop in T.GetType().GetProperties())
- {
- dc.Add(prop.Name, prop.GetValue(T));
- }
- /*
- foreach (PropertyInfo prop in e.GetType().GetRuntimeProperties())
- {
- dc[prop.Name]
- }
- */
- if (dc.ContainsKey("Name"))
- {
- //e.Name = (string)dc["Name"];
- this.Name = (string)dc["Name"];
- }
- if (dc.ContainsKey("Surname"))
- {
- //e.Surname = (string)dc["Surname"];
- this.Surname = (string)dc["Surname"];
- }
- // jest problem z rzutowaniem
- /*
- if (dc.ContainsKey("Salary"))
- {
- //e.Salary = (decimal)dc["Salary"];
- this.Salary = (decimal)dc["Salary"];
- }
- */
- if (dc.ContainsKey("bornDate"))
- {
- // e.bornDate = (DateTime)dc["bornDate"];
- this.bornDate = (DateTime)dc["bornDate"];
- }
- if (dc.ContainsKey("Weight"))
- {
- //e.Weight = (double)dc["Weight"];
- this.Weight = (double)dc["Weight"];
- }
- if (dc.ContainsKey("IsAvailable"))
- {
- //e.IsAvailable = (bool)dc["IsAvailable"];
- this.IsAvailable = (bool)dc["IsAvailable"];
- }
- //return e
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var cos = new { Surname = "Kowalski", Salary = 2110, IsAvailable = false };
- Employee emp = new Employee(cos);
- Console.WriteLine(emp.Surname);
- Console.WriteLine(emp.Salary);
- Console.WriteLine(emp.IsAvailable);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement