{"record":{"id":"146d6aeefa739e3c","repo":"stride3d/stride","slug":"factory-serializerfactoryselector","errorCode":null,"errorMessage":"factory","messagePattern":"factory","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/Serialization/SerializerFactorySelector.cs","lineNumber":23,"sourceCode":"using System.Threading;\nusing Stride.Core.Reflection;\n\nnamespace Stride.Core.Yaml.Serialization\n{\n    /// <summary>\n    /// Base class that implements <see cref=\"ISerializerFactorySelector\"/>.\n    /// </summary>\n    public abstract class SerializerFactorySelector : ISerializerFactorySelector\n    {\n        private readonly Dictionary<Type, IYamlSerializable> serializers = new Dictionary<Type, IYamlSerializable>();\n        private readonly List<IYamlSerializableFactory> factories = new List<IYamlSerializableFactory>();\n        private readonly ReaderWriterLockSlim serializerLock = new ReaderWriterLockSlim();\n        private bool isSealed;\n\n        /// <inheritdoc/>\n        public void TryAddFactory(IYamlSerializableFactory factory)\n        {\n            if (factory == null) throw new ArgumentNullException(nameof(factory));\n            if (isSealed) throw new InvalidOperationException(\"Cannot add a factory to a serializer factory selector once it is sealed.\");\n            if (CanAddSerializerFactory(factory))\n            {\n                factories.Add(factory);\n            }\n        }\n\n        /// <inheritdoc/>\n        public void Seal()\n        {\n            isSealed = true;\n        }\n\n        /// <inheritdoc/>\n        public IYamlSerializable GetSerializer(SerializerContext context, ITypeDescriptor typeDescriptor)\n        {\n            if (!isSealed) throw new InvalidOperationException(\"A serializer factory selector must be sealed before being used.\");\n            IYamlSerializable serializer;","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/Serialization/SerializerFactorySelector.cs#L5-L41","documentation":"SerializerFactorySelector.TryAddFactory throws ArgumentNullException(\"factory\") when a null IYamlSerializableFactory is registered. Factories must be non-null so the selector can later query them to produce serializers.","triggerScenarios":"Calling TryAddFactory(null), usually because a factory instance field was never initialized or a DI container resolved to null.","commonSituations":"Optional custom serializer factories that failed to construct; configuration-driven registration where the type name resolved to null; ordering bugs where registration happens before construction.","solutions":["Pass a valid IYamlSerializableFactory instance.","Fix the construction/resolution path that produced null.","Guard registration code with a null check and log/skip.","Catch ArgumentNullException around registration during plugin loading.","Register factories explicitly with `new` rather than via possibly-null variables."],"exampleFix":"// before\nselector.TryAddFactory(customFactory); // null\n// after\nif (customFactory != null) selector.TryAddFactory(customFactory);","handlingStrategy":"validation","validationCode":"if (factory == null) throw new ArgumentException(\"Serializer factory must not be null\", nameof(factory));","typeGuard":"static bool ValidFactory(IYamlSerializableFactory f) => f != null;","tryCatchPattern":"try { selector.TryAddFactory(factory); }\ncatch (ArgumentNullException ex) { log.Warn(\"Skipping null serializer factory registration\", ex); }","preventionTips":["Eagerly construct factories at startup","Use `?? throw` at DI resolution sites","Never store factories in nullable fields without checks"],"tags":["yaml","null-argument","serializer-factory"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}