Advertisement
DigitalMag

Interface Sample (C#)

Mar 13th, 2020
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     public interface IModel
  10.     {
  11.         IModel Create(object obj);
  12.     }
  13.  
  14.     public class Contact
  15.     {
  16.  
  17.         public string Name { get; set; }
  18.  
  19.         public string PhotoUriThumbnail { get; set; }
  20.  
  21.         public string Number { get; set; }
  22.  
  23.         public List<string> Numbers { get; set; }
  24.  
  25.     }
  26.  
  27.  
  28.  
  29.     public class ContactTarget : IModel
  30.     {
  31.  
  32.         public string Phone { get; set; }
  33.         public string Name { get; set; }
  34.         public string Photo { get; set; }
  35.         public string OptionalPhones { get; set; }
  36.  
  37.  
  38.         public IModel Create(object __contact)
  39.         {
  40.             var contact = __contact as Contact;
  41.            
  42.             this.Name = contact.Name;
  43.             this.Phone = contact.Number;
  44.             this.OptionalPhones = Convert.ToBoolean(contact.Numbers) ? string.Join(";", contact.Numbers) : string.Empty;
  45.             this.Photo = contact.PhotoUriThumbnail ?? string.Empty;
  46.  
  47.             return this;
  48.         }
  49.  
  50.         public static implicit operator ContactTarget(Contact contact)
  51.         {
  52.             var _contact = new ContactTarget();
  53.             _contact.Name = contact.Name;
  54.             _contact.Phone = contact.Number;
  55.             _contact.OptionalPhones = Convert.ToBoolean(contact.Numbers) ? string.Join(";", contact.Numbers) : string.Empty;
  56.             _contact.Photo = contact.PhotoUriThumbnail ?? string.Empty;
  57.  
  58.             return _contact;
  59.         }//*/
  60.  
  61.  
  62.         public void Log()
  63.         {
  64.             Console.WriteLine(this.Name);
  65.         }
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.     class Program
  72.     {
  73.  
  74.         static List<TOut> iConvert<TOut>(List<object> raw) where TOut : IModel, new()
  75.         {
  76.             var res = new List<TOut>();
  77.             for (int i = 0; i < raw.Count; i++)
  78.             {
  79.                 var r = new TOut().Create(raw[i]);
  80.                 res.Add((TOut)r);
  81.             }
  82.             return res;
  83.         }
  84.  
  85.  
  86.  
  87.         static void Main(string[] args)
  88.         {
  89.  
  90.             var a = new List<object>()
  91.             {
  92.                 new Contact { Name="vazc", Number="9" },
  93.                 new Contact { Name="vazc" },
  94.                 new Contact { Name="vazc", Number="9" }
  95.             };
  96.  
  97.             // var b = a.Select(i => (ContactTarget)(Contact)i).ToList();            
  98.             var b = iConvert<ContactTarget>(a);
  99.  
  100.             b[0].Log();
  101.  
  102.             Console.Read();
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement