Multiple Docs: Safely accessing stored RhinoDoc reference

Is it safe to store a reference to a document and access it later on?

public class Foo
{
    public RhinoDoc Document { get; private set; }
    public MyStore MyStore { get => Document.RuntimeData.GetValue(typeof(MyStore), rhinoDoc => new MyStore(rhinoDoc)); }

    public Foo(RhinoDoc rhinoDoc) : base()
    {
        Document = rhinoDoc;
    }

    public void Bar() {
        MyStore.Xyz();
    }
}

Or should I always retrieve the document from the serialnumber?

public class Foo
{
    public uint DocumentSerialNumber { get; private set; }
    public RhinoDoc Document { get => RhinoDoc.FromRuntimeSerialNumber(DocumentSerialNumber); }
    public MyStore MyStore { get => Document.RuntimeData.GetValue(typeof(MyStore), rhinoDoc => new MyStore(rhinoDoc)); }

    public Foo(unit documentSerialNumber) : base()
    {
        DocumentSerialNumber = documentSerialNumber;
    }

    public void Bar() {
        MyStore.Xyz();
    }
}

What are the performance implications of each, if there are any? It seems expensive to look it up every time :man_shrugging:

If the document is created by you, I think the first one is enough.

I’d use WeakReference though.

Thanks for the reply!

It’s not. It’s the actual “user” controlled documents. Do you know if a reference to one of them will be valid as long as the user have that document open?

FromRuntimeSerialNumber is pretty fast. You can see the implementation of it at

Unfortunately no. If a user closes the doc, the reference will no longer be valid.

1 Like

Moved everything to FromRuntimeSerialNumber. Thanks for the enlightenment Steve!