Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Test
- {
- public class Contract
- {
- public uint Id { get; set; }
- public string Number { get; set; }
- public DateTime DateStart { get; set; }
- }
- public class GenericObj
- {
- public Dictionary<string, object> PropertiesNameValueCollection { get; set; } = new Dictionary<string, object>();
- }
- public class MySqlService
- {
- private string ConStr;
- public MySqlService(string conStr)
- {
- ConStr = conStr;
- }
- public List<GenericObj> ExecuteReader(string sql)
- {
- var list = new List<GenericObj>();
- using (var con = new MySqlConnection(ConStr))
- using (var cmd = new MySqlCommand(sql, con))
- {
- con.Open();
- var reader = cmd.ExecuteReader();
- while (reader.Read())
- {
- var count = reader.FieldCount;
- var obj = new GenericObj();
- for (int i = 0; i < count; i++)
- {
- obj.PropertiesNameValueCollection.Add(reader.GetName(i), reader.GetValue(i));
- }
- list.Add(obj);
- }
- }
- return list;
- }
- }
- public class Resolver<T>
- {
- public T Resolve(GenericObj obj)
- {
- var type = typeof(T);
- T specificObj = (T)Activator.CreateInstance(type);
- foreach(var field in obj.PropertiesNameValueCollection)
- {
- type.GetProperty(field.Key).SetValue(specificObj, field.Value);
- }
- return specificObj;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var service = new MySqlService("constr");
- var result = service.ExecuteReader("SELECT mai_id as Id, mai_no_d as Number, mai_date_start as DateStart FROM main");
- var list = new List<Contract>();
- var resolver = new Resolver<Contract>();
- foreach (var genObj in result)
- {
- list.Add(resolver.Resolve(genObj));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement