CloneObjectViewController.CustomCloneObject Event
Occurs before the default cloning process begins.
Namespace: DevExpress.ExpressApp.CloneObject
Assembly: DevExpress.ExpressApp.CloneObject.Xpo.v24.1.dll
NuGet Package: DevExpress.ExpressApp.CloneObject.Xpo
Declaration
Event Data
The CustomCloneObject event's data class is CustomCloneObjectEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
ClonedObject | Represents the target object of the cloning process. |
SourceObject | Gets the object to be cloned. |
TargetObjectSpace | Specifies the Object Space of the target object. |
TargetType | Gets the type of the target object. |
The event data class exposes the following methods:
Method | Description |
---|---|
CreateDefaultTargetObjectSpace() | Returns an Object Space for the CustomCloneObjectEventArgs.ClonedObject. |
Remarks
When this event is handled and the handler’s ClonedObject parameter is initialized, the object represented by this parameter becomes the target object of the cloning process. Otherwise, default cloning is performed.
Handle this event to implement custom cloning logic. Pass the custom cloned object via the handler’s ClonedObject parameter. Pass the target object’s Object Space via the TargetObjectSpace parameter.
To manually clone a source object, handle this event and assign a custom cloned object to the CustomCloneObjectEventArgs.ClonedObject parameter. If the event is not handled or the ClonedObject parameter is null (Nothing in VB), the default cloning is performed.
By default, the CustomCloneObjectEventArgs.TargetObjectSpace parameter refers to an IObjectSpace object. Initialize the TargetObjectSpace property to use custom Object Space type object to clone an object.
Note
When the handler’s ClonedObject parameter is passed, but the TargetObjectSpace parameter is null (Nothing in VB), an exception is raised.
You can implement a custom DevExpress.Persistent.Base.Cloner class descendant to customize the cloning process, and then call it’s CloneTo method in the CustomCloneObject event handler.
The following snippet illustrates how to override the Cloner.CopyMemberValue method to skip association proprerties when cloning.
using DevExpress.ExpressApp.CloneObject;
using DevExpress.Xpo.Metadata;
using DevExpress.Xpo;
using DevExpress.Persistent.Base;
//...
public class CustomizeCloneObjectController : ObjectViewController {
protected override void OnActivated() {
base.OnActivated();
var cloneObjectController = Frame.GetController<CloneObjectViewController>();
if (cloneObjectController != null) {
cloneObjectController.CustomCloneObject += cloneObjectController_CustomCloneObject;
}
}
void cloneObjectController_CustomCloneObject(object sender, CustomCloneObjectEventArgs e) {
var cloner = new MyCloner();
e.TargetObjectSpace = e.CreateDefaultTargetObjectSpace();
object objectFromTargetObjectSpace = e.TargetObjectSpace.GetObject(e.SourceObject);
e.ClonedObject = cloner.CloneTo(objectFromTargetObjectSpace, e.TargetType);
}
}
public class MyCloner : Cloner {
public override void CopyMemberValue(
XPMemberInfo memberInfo, IXPSimpleObject sourceObject, IXPSimpleObject targetObject) {
if (!memberInfo.IsAssociation) {
base.CopyMemberValue(memberInfo, sourceObject, targetObject);
}
}
}