vendredi 19 mai 2023

How to fill current object (this) by another object

I need to copy all properties from another object to curren object (this) in Kotlin. object.copy() is not solution because i can't use it on curren instance of class. Maybe by reflection. In c# it looks like this:

public virtual void fillFromObject(object obj){
    foreach (var property in this.GetType().GetProperties().ToList())
    {
        if (property.GetSetMethod() != null)
            property.SetValue(this, obj.GetType().GetProperty(property.Name).GetValue(obj, null), null);
    }
}

using:

this.fillFromObject(anotherObject);




Aucun commentaire:

Enregistrer un commentaire