{"record":{"id":"b9965c69a3b55d24","repo":"dotnet/wpf","slug":"sr-format-sr-propertymusthavevalue-datatype-this-gettype","errorCode":null,"errorMessage":"SR.Format(SR.PropertyMustHaveValue, \"DataType\", this.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.PropertyMustHaveValue, \"DataType\", this\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateKey.cs","lineNumber":50,"sourceCode":"\n            _dataType = dataType;\n            _templateType = templateType;\n        }\n\n#region ISupportInitialize\n\n        /// <summary>Begin Initialization</summary>\n        void ISupportInitialize.BeginInit()\n        {\n            _initializing = true;\n        }\n\n        /// <summary>End Initialization, verify that internal state is consistent</summary>\n        void ISupportInitialize.EndInit()\n        {\n            if (_dataType == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.PropertyMustHaveValue, \"DataType\", this.GetType().Name));\n            }\n\n            _initializing = false;\n        }\n\n#endregion ISupportInitialize\n\n        /// <summary>\n        /// The type for which the template is designed.  This is either\n        /// a Type (for object data), or a string (for XML data).  In the latter\n        /// case the string denotes the XML tag name.\n        /// </summary>\n        public object DataType\n        {\n            get { return _dataType; }\n            set\n            {\n                if (!_initializing)","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateKey.cs#L32-L68","documentation":"TemplateKey implements ISupportInitialize; EndInit validates that the DataType property was set during the initialization block. If BeginInit was called but DataType was never assigned, EndInit throws InvalidOperationException because a template key without a DataType cannot be used to look up implicit templates.","triggerScenarios":"Calling BeginInit() on a TemplateKey (or a TemplateNameKey/TypeDataKey subclass) and then EndInit() without ever setting the DataType property in between.","commonSituations":"Hand-written XAML parser or tooling that creates template keys programmatically and forgets to set DataType before finishing initialization; deserializers that call the initialization pattern but skip optional properties.","solutions":["Set the DataType property (a Type or string) after BeginInit() and before calling EndInit().","If the key is not meant to be an implicit template key, construct it with the constructor overload that takes a template name instead of using the BeginInit/EndInit pattern.","Wrap EndInit in a try-catch for InvalidOperationException when the DataType is legitimately absent, and fall back to a named template key."],"exampleFix":"// before\nvar key = new TemplateKey();\nkey.BeginInit();\nkey.EndInit(); // throws\n// after\nvar key = new TemplateKey();\nkey.BeginInit();\nkey.DataType = typeof(MyViewModel);\nkey.EndInit();","handlingStrategy":"validation","validationCode":"if (key is ISupportInitialize si && key.GetType().GetProperty(\"DataType\")?.GetValue(key) == null)\n    key.GetType().GetProperty(\"DataType\")?.SetValue(key, typeof(Target));\nsi?.EndInit();","typeGuard":null,"tryCatchPattern":"try { si.EndInit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DataType\")) { /* substitute named key or log */ }","preventionTips":["Always pair BeginInit with setting DataType and EndInit in the same scope.","Prefer constructors that take the required data over the initialization pattern when possible.","Add an assertion that DataType != null immediately before EndInit."],"tags":["wpf","initialization","invalid-operation"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}