简体   繁体   中英

C# Clone same properties from Derived class to base class

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 !

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)
    {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM