2

I am struggling to find a safe way to clone same properties from derived class to base class, I already have one method, cloning base class properties to derived class

protected internal void InitInhertedProperties(object baseClassInstance)
    {
        foreach (PropertyInfo propertyInfo in baseClassInstance.GetType().GetProperties())
        {
            object value = propertyInfo.GetValue(baseClassInstance, null);
            if (null != value) propertyInfo.SetValue(this, value, null);
        }
    }

But what about reverse cloning ? using a method or a library to clone same properties of derived class to that of base class.

Base Class

public class UserEntity
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }
    public int employee_id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string email { get; set; }
    public string login_id { get; set; }
    public string login_password { get; set; }
    public int role { get; set; }
    public bool is_delete { get; set; }
}

Derived Class

public class UserModel : UserEntity
{

    protected internal void InitInhertedProperties(object baseClassInstance)
    {
        foreach (PropertyInfo propertyInfo in baseClassInstance.GetType().GetProperties())
        {
            object value = propertyInfo.GetValue(baseClassInstance, null);
            if (null != value) propertyInfo.SetValue(this, value, null);
        }
    }
}

Instead of doing the way below:

var user_entity = new UserEntity();
        user_entity.id = user_model.id;
        user_entity.employee_id = user_model.employee_id;
        user_entity.first_name = user_model.first_name;
        user_entity.email = user_model.email;
        user_entity.login_id = user_model.login_id;
        user_entity.login_password = user_model.login_password;
        user_entity.role = user_model.role;
        user_entity.is_delete = false;

Thanks !

Steven Li
  • 754
  • 1
  • 8
  • 16
  • 1
    For tasks like this I use AutoMapper (http://automapper.org/) – EluciusFTW Mar 05 '17 at 09:48
  • 1
    I use protected copy constructors (like in [this answer](http://stackoverflow.com/a/1573594/106159)) for copying objects in a hierachy. I avoid reflection for stuff like this - it sometimes doesn't work at all well when you want deep-copy semantics. Automapper is OK for DTOs, but for other types it's not so good (particularly not for things in a class hierarch). – Matthew Watson Mar 05 '17 at 09:54
  • @Evk isn't `FlattenHierarchy` for use with static members only? – pinkfloydx33 Mar 05 '17 at 10:22
  • I am confused. Are you trying to get `UserEntity` based on `UserModel` or the other way around? Or both? – hyankov Mar 05 '17 at 10:25
  • @pinkfloydx33 it seems you are right. I was under impression it works a bit differentely but now not sure so deleted comment to not confuse people. – Evk Mar 05 '17 at 10:30

1 Answers1

1
public class UserEntity
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }
    public int employee_id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string email { get; set; }
    public string login_id { get; set; }
    public string login_password { get; set; }
    public int role { get; set; }
    public bool is_delete { get; set; }

    public UserEntity()
    {
    }

    public UserEntity(UserEntity userEntity)
    {
        this.id = userEntity.id;
        this.employee_id = userEntity.employee_id;
        this.first_name = userEntity.first_name;
        this.email = userEntity.email;
        this.login_id = userEntity.login_id;
        this.login_password = userEntity.login_password;
        this.role = userEntity.role;
        this.is_delete = false;
    }
}

public class UserModel : UserEntity
{
    public UserModel(UserEntity userEntity) : base(userEntity)
    {
    }
}
hyankov
  • 4,049
  • 1
  • 29
  • 46
  • You should really follow the `protected copy constructor` model for this, and provide a public virtual `Copy()` method that calls it. Then you can make a copy of a derived class through the `Copy()` method - if you use a public copy constructor you can end up slicing the objects if you try to make a copy of a derived class object via the base class type. – Matthew Watson Mar 05 '17 at 09:56
  • @MatthewWatson, perhaps you are right. Could you post it as a complete code, so we can compare the two solutions and more clearly see the benefit of your suggestion? – hyankov Mar 05 '17 at 10:24
  • There's sample code in the link I posted in a reply to the OP. – Matthew Watson Mar 05 '17 at 10:57