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 !