SHOW:
|
|
- or go back to the newest paste.
| 1 | ||
| 2 | - | public class Animal |
| 2 | + | namespace ConsoleApplication1 |
| 3 | {
| |
| 4 | - | public T Promote<T>() where T : Animal |
| 4 | + | //[Serializable] |
| 5 | public class Animal //: ICloneable | |
| 6 | - | return this as T; |
| 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 | - | |
| 11 | + | foreach (var member in members.Where(m => m.MemberType == MemberTypes.Property)) |
| 12 | clone | |
| 13 | - | public class test |
| 13 | + | .GetType() |
| 14 | .GetProperty(member.Name) | |
| 15 | - | public void ttt() |
| 15 | + | .SetValue(clone, GetType().GetProperty(member.Name).GetValue(this, null), null); |
| 16 | return clone; | |
| 17 | - | Animal animal = new Animal(); |
| 17 | + | |
| 18 | - | Dog dog = animal.Promote<Dog>(); |
| 18 | + | |
| 19 | ||
| 20 | - | } |
| 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 | } |