What is the best practice for changing the properties of a base object from an inherited class

I want to set the "person" properties for student1 to person1. Is it possible to do this with an assignment or in any way without setting properties one by one?

static void Main(string[] args)
{
  var student1 = new Student {Id = 1, Name = "kaya", Class = "3b", Number = "156"};
            var person1 = new Person { Id = 2, Name = "hasan" };

        }


    public class Person
    {
        public int Id { get; set; }
        public String Name { get; set; }
    }

    public class Student : Person
    {
        public int Number { get; set; }
        public String Class { get; set; }
    }
+3
source share
2 answers

Well, you could do it with a reflection, but I personally did not. You can add a method CopyToto Person:

public class Person
{
    public int Id { get; set; }
    public String Name { get; set; }

    public void CopyTo(Person other)
    {
        other.Id = Id;
        other.Name = Name;
    }
}

- , Student Person , Person. - , Student Person, ?

, , ... , , , d . , , , , .

+4

AutoMapper? , -

http://automapper.codeplex.com/

AutoMapper POCO Subsonic.

0

Source: https://habr.com/ru/post/1728803/


All Articles