CollectionHelperConvertItemTSource, TDestination Method |
Namespace: Xcalibur.Helpers
public static TDestination ConvertItem<TSource, TDestination>( this TSource item ) where TDestination : new()
1// A person model 2public class Person 3{ 4 public string Name { get; set; } 5 public string Address { get; set; } 6 public string City { get; set; } 7 public string State { get; set; } 8 public int ZipCode { get; set; } 9} 10 11// A shorter person model 12public class PersonShort 13{ 14 public string Name { get; set; } 15 public string Address { get; set; } 16 public int ZipCode { get; set; } 17} 18 19// Main 20static void Main(string[] args) 21{ 22 // Create a new 23 var person = new Person 24 { 25 Name = "Steve The Machine", 26 Address = "123 Some Street", 27 City = "Someplace", 28 State = "AL", 29 ZipCode = 11111 30 }; 31 32 // Get a new PersonShort from Person 33 var personShort = person.ConvertItem<Person, PersonShort>(); 34 Console.WriteLine(personShort.Name); 35 Console.WriteLine(personShort.Address); 36 Console.WriteLine(personShort.ZipCode); 37 38 // Hold 39 Console.Read(); 40} 41 42/* Result: 43 * 44 * Steve The Machine 45 * 123 Some Street 46 * 11111 47 */