C# The object cannot be modified because it is controlled by a document

Hi, i’ve written the following script inside a grasshopper “C# Script” component.

But sometimes it gives this error: "The object cannot be modified because it is controlled by a document"

I not understand why this exception is thrown since my script does not modify any property of the layer.

This error does not appear every time and I’m not able to find a way to reproduce it.
The only way to solve is to restart rhino.

I’m using Grasshopper over a rhino worksession with some files attached.

Any ideas?

private void RunScript(ref object A)
   List<string> layerList = new List<string>();

   RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
   foreach(Layer l in doc.Layers)
   A  = layerList;

In my experience, this exception is thrown not because the object is document controlled, but because it has been disposed already as part of a document operation (e.g. layer-delete). To see if the object has been disposed, this can be tested using the IsDisposed property.

Hi, where I can find the IsDisposed property? I’m not able to find it in documentation and/or c# editor suggestion

The property is actually called Disposed.

See the documentation for layer.