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"))
- {
- if (dc["Name"].GetType() == typeof(string))
- {
- //e.Name = (string)dc["Name"];
- this.Name = (string)dc["Name"];
- }
- }
- if (dc.ContainsKey("Surname"))
- {
- if (dc["Surname"].GetType() == typeof(string))
- {
- //e.Surname = (string)dc["Surname"];
- this.Surname = (string)dc["Surname"];
- }
- }
- // jest problem z rzutowaniem
- if (dc.ContainsKey("Salary"))
- {
- if (dc["Salary"].GetType() == typeof(decimal))
- {
- //e.Salary = (decimal)dc["Salary"];
- this.Salary = (decimal)dc["Salary"];
- }
- }
- if (dc.ContainsKey("bornDate"))
- {
- if (dc["bornDate"].GetType() == typeof(DateTime))
- {
- // e.bornDate = (DateTime)dc["bornDate"];
- this.bornDate = (DateTime)dc["bornDate"];
- }
- }
- if (dc.ContainsKey("Weight"))
- {
- if (dc["Weight"].GetType() == typeof(double))
- {
- //e.Weight = (double)dc["Weight"];
- this.Weight = (double)dc["Weight"];
- }
- }
- if (dc.ContainsKey("IsAvailable"))
- {
- if (dc["IsAvailable"].GetType() == typeof(bool))
- {
- //e.IsAvailable = (bool)dc["IsAvailable"];
- this.IsAvailable = (bool)dc["IsAvailable"];
- }
- }
- //return e
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var cos = new {Name = "Jan", Surname = "Kowalski", Salary = 2000, IsAvailable = "dsadas", Weight = 20.9d };
- Employee emp = new Employee(cos);
- Console.WriteLine(cos.Name.GetType());
- Console.WriteLine(emp.Name.GetType());
- Console.WriteLine(emp.Name);
- Console.WriteLine(emp.Surname);
- Console.WriteLine(emp.Salary);
- Console.WriteLine(emp.IsAvailable);
- Console.WriteLine(emp.Weight);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement