DevExpress XAF / XPO getobjectbyid / key clone / copy members

Date: 2023-04-26
public class ConfigurationItemCloner : Cloner
{
    private readonly List<string> _dontCloneFields;
    private readonly List<DXPropertyEditor> _propertyEditors;

    public ConfigurationItemCloner(
        List<string> dontCloneFields,
        List<DXPropertyEditor> propertyEditors
    )
    {
        _dontCloneFields = dontCloneFields;
        _propertyEditors = propertyEditors;
    }

    public override void CopyMemberValue(
        XPMemberInfo memberInfo,
        IXPSimpleObject sourceObject,
        IXPSimpleObject targetObject
    )
    {
        var customPropertyEditor = _propertyEditors.FirstOrDefault(
            x => x.PropertyName == memberInfo.Name
        );

        if (_dontCloneFields == null || !_dontCloneFields.Contains(memberInfo.Name))
        {
            if ((sourceObject is Configuratieitem) && customPropertyEditor != null)
            {
                var session = targetObject.Session;
                var newValue = customPropertyEditor.PropertyValue;
                if (newValue is PersistentBase obj)
                {
                    var key = obj.ClassInfo.GetId(obj);
                    newValue = session.GetObjectByKey(newValue.GetType(), key);
                }
                targetObject
                    .GetType()
                    .GetProperty(customPropertyEditor.PropertyName)
                    .SetValue(targetObject, newValue);
            }
            else
            {
                base.CopyMemberValue(memberInfo, sourceObject, targetObject);
            }
        }
    }
}
77080cookie-checkDevExpress XAF / XPO getobjectbyid / key clone / copy members