DefconDotNet

Untitled

Dec 11th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. namespace ConsoleApplication1
  3. {
  4.     //[Serializable]
  5.     public class Animal //: ICloneable
  6.     {
  7.         public T Promote<T>() where T : new()
  8.         {
  9.             var clone = new T();
  10.             var members = GetType().GetMembers(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
  11.             foreach (var member in members.Where(m => m.MemberType == MemberTypes.Property))
  12.                 clone
  13.                     .GetType()
  14.                     .GetProperty(member.Name)
  15.                     .SetValue(clone, GetType().GetProperty(member.Name).GetValue(this, null), null);
  16.             return clone;
  17.         }
  18.     }
  19.  
  20.     //[Serializable]
  21.     public class Dog : Animal
  22.     {
  23.     }
  24.    
  25.     public class Program
  26.     {
  27.         private static void Main(string[] args)
  28.         {
  29.             var animal = new Animal();
  30.             var dog = animal.Promote<Dog>();
  31.  
  32.             Console.WriteLine(dog==null);
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment