NeptuneBspEphemerisProviderClone Method |
Namespace: AGI.Foundation.Spice
This method should be implemented to call a copy constructor on the class of the object being cloned. The copy constructor should take the CopyContext as a parameter in addition to the existing instance to copy. The copy constructor should first call AddObjectMappingT(T, T) to identify the newly constructed instance as a copy of the existing instance. It should then copy all fields, using UpdateReferenceT(T) to copy any reference fields.
A typical implementation of ICloneWithContext:
public class MyClass : ICloneWithContext { public MyClass(MyClass existingInstance, CopyContext context) { context.AddObjectMapping(existingInstance, this); someReference = context.UpdateReference(existingInstance.someReference); } public object Clone(CopyContext context) { return new MyClass(this, context); } private object someReference; }
In general, all fields that are reference types should be copied with a call to UpdateReferenceT(T). There are a couple of exceptions:
If one of these exceptions applies, the CopyContext should be given an opportunity to update the reference before the reference is copied explicitly. Use UpdateReferenceT(T) to update the reference. If UpdateReferenceT(T) returns the original object, indicating that the context does not have a replacement registered, then copy the object manually by invoking a Clone method, a copy constructor, or by manually constructing a new instance and copying the values.
alwaysCopy = context.UpdateReference(existingInstance.alwaysCopy); if (existingInstance.alwaysCopy != null && ReferenceEquals(alwaysCopy, existingInstance.alwaysCopy)) { alwaysCopy = existingInstance.alwaysCopy.Clone(context) as AlwaysCopy; }
If you are implementing an evaluator (a class that implements IEvaluator), the UpdateEvaluatorReferences(CopyContext) method shares some responsibilities with the copy context constructor. Code duplication can be avoided by doing the following:
public MyClass(MyClass existingInstance, CopyContext context) : base(existingInstance, context) { someReference = context.UpdateReference(existingInstance.someReference); evaluatorReference = existingInstance.evaluatorReference; UpdateEvaluatorReferences(context); } public override void UpdateEvaluatorReferences(CopyContext context) { evaluatorReference = context.UpdateReference(evaluatorReference); } public override object Clone(CopyContext context) { return new MyClass(this, context); } private object someReference; private IEvaluator evaluatorReference;