internal ImmutableSettings ToImmutable()
        {
            var result = new ImmutableSettings(
                SerializationMode,
                AllowFunctionSerialization,
                SupportSerializationHooks,
                InliningMaxDepth,
                UseSerializedVersionId,
                FlattenClassHierarchy,
                ForceReflectionToSetReadonlyFields,
                CustomActionSerializers,
                CustomActionDeserializers,
                WhitelistedTypes,
                WhitelistFuncs);

            lock (_constructedSettingsLock)
            {
                if (_constructedSettings.TryGetValue(result, out var previousConstructed))
                {
                    return(previousConstructed);
                }
                _constructedSettings.Add(result, result);
            }

            return(result);
        }
#pragma warning disable CS8618 // Non-nullable field is uninitialized.
        internal Binary(ImmutableSettings settings, TStream stream)
#pragma warning restore CS8618 // Non-nullable field is uninitialized.
        {
            Settings      = settings;
            _binaryWriter = new BinaryWriter <TStream, TSettingGen>(this);
            _binaryReader = new BinaryReader <TStream, TSettingGen>(this);
            _stream       = stream;
            if (Settings.SerializationMode == Mode.Graph)
            {
                _savedObjectLookup = new DictionarySlim <object, int>(16);
                _loadedObjectRefs  = new List <object>(16);
            }

            if (Settings.SupportSerializationHooks)
            {
                _deserializationHooks = new List <ValueTuple <Action <object, object>, object> >();
            }
        }