Sorry, I had forgotten to subscribe to this post so I missed your request.
Below is my solution to the issue. Hopefully it will work for you as well. It is based of the sample that koganam provided and an addition sample can be found in @PriceLevels.cs. I believe @Pricelevels has a hacky workaround for the collection though - you'll probably see what I mean below.
The Problem
Before coming up with a solution, it's always good to know exactly what the problem is. Basically, the issue comes down to copying instances of objects in one assembly to instances of objects in another assembly. When you "Reload Ninjascript" on a chart, what NT does is load another instance of the custom DLL and clones all the object instances in the currently loaded custom DLL to the instances in the newly loaded DLL.
This gives rise to two distinct scenarios (let's call the current assembly A and the new assembly B):
- Reload Ninjascript without recompiling
- A and B refer to exactly the same assembly
- Reload Ninjascript after recompiling
- A and B refer to different assemblies (even if the code did not change)
Most problems stem from the second scenario. Code executing in an assembly will only use types in that assembly when the types are statically referenced. In order to use types from another version of the assembly, reflection is needed.
e.g.
[LEFT][COLOR=#4D4D4D][FONT=Helvetica]var a = new Person();[/FONT][/COLOR][/LEFT] [LEFT][COLOR=#4D4D4D][FONT=Helvetica][/FONT][/COLOR][/LEFT]
[LEFT][COLOR=#4D4D4D][FONT=Helvetica]var a = new Person();[/FONT][/COLOR][/LEFT] [LEFT][COLOR=#4D4D4D][FONT=Helvetica][/FONT][/COLOR][/LEFT]
The Solution
There are two main entry points to the NT cloning logic: Implementing ICloneable and Indicator.CopyTo(NinjaScript script). You'll want to use ICloneable for individual objects and CopyTo for collections of objects.
So, the solution comes down to:
- Cloning
- Create a new instance of the corresponding type in B
- Copy the properties of the instance in A to the new B instance (May need to clone property values as well)
- CopyTo
- Clone the collection items in A to the corresponding type in B as described in step 1
- Get the collection in B and add the cloned items
This post is a bit long so I'll post the implementation of the solution in a different post below.
Thanks,
Wil
Comment