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;
- namespace ConsoleApplication1
- {
- public interface IModel
- {
- IModel Create(object obj);
- }
- public class Contact
- {
- public string Name { get; set; }
- public string PhotoUriThumbnail { get; set; }
- public string Number { get; set; }
- public List<string> Numbers { get; set; }
- }
- public class ContactTarget : IModel
- {
- public string Phone { get; set; }
- public string Name { get; set; }
- public string Photo { get; set; }
- public string OptionalPhones { get; set; }
- public IModel Create(object __contact)
- {
- var contact = __contact as Contact;
- this.Name = contact.Name;
- this.Phone = contact.Number;
- this.OptionalPhones = Convert.ToBoolean(contact.Numbers) ? string.Join(";", contact.Numbers) : string.Empty;
- this.Photo = contact.PhotoUriThumbnail ?? string.Empty;
- return this;
- }
- public static implicit operator ContactTarget(Contact contact)
- {
- var _contact = new ContactTarget();
- _contact.Name = contact.Name;
- _contact.Phone = contact.Number;
- _contact.OptionalPhones = Convert.ToBoolean(contact.Numbers) ? string.Join(";", contact.Numbers) : string.Empty;
- _contact.Photo = contact.PhotoUriThumbnail ?? string.Empty;
- return _contact;
- }//*/
- public void Log()
- {
- Console.WriteLine(this.Name);
- }
- }
- class Program
- {
- static List<TOut> iConvert<TOut>(List<object> raw) where TOut : IModel, new()
- {
- var res = new List<TOut>();
- for (int i = 0; i < raw.Count; i++)
- {
- var r = new TOut().Create(raw[i]);
- res.Add((TOut)r);
- }
- return res;
- }
- static void Main(string[] args)
- {
- var a = new List<object>()
- {
- new Contact { Name="vazc", Number="9" },
- new Contact { Name="vazc" },
- new Contact { Name="vazc", Number="9" }
- };
- // var b = a.Select(i => (ContactTarget)(Contact)i).ToList();
- var b = iConvert<ContactTarget>(a);
- b[0].Log();
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement