{"record":{"id":"5e655168fdb065f2","repo":"stride3d/stride","slug":"tag-not-found-in-template-generator-parameters","errorCode":null,"errorMessage":"Tag not found in template generator parameters","messagePattern":"Tag not found in template generator parameters","errorType":"exception","errorClass":"KeyNotFoundException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorParameters.cs","lineNumber":183,"sourceCode":"    /// Validates this instance (all fields must be setup)\n    /// </summary>\n    public void Validate()\n    {\n        ValidateParameters();\n    }\n\n    /// <summary>\n    /// Gets the tag corresponding to the given property key. This will fail if key doesn't exist.\n    /// </summary>\n    /// <typeparam name=\"T\">The generic type of the property key.</typeparam>\n    /// <param name=\"key\">The property key for which to retrieve the value.</param>\n    /// <returns>The value of the tag corresponding to the given property key.</returns>\n    /// <exception cref=\"KeyNotFoundException\">Tag not found in template generator parameters.</exception>\n    public T GetTag<T>(PropertyKey<T> key)\n    {\n        if (!Tags.TryGetValue(key, out var result))\n        {\n            throw new KeyNotFoundException(\"Tag not found in template generator parameters\");\n        }\n        return result;\n    }\n\n    /// <summary>\n    /// Gets the tag corresponding to the given property key if available, otherwise returns default value.\n    /// </summary>\n    /// <typeparam name=\"T\">The generic type of the property key.</typeparam>\n    /// <param name=\"key\">The property key for which to retrieve the value.</param>\n    /// <returns>The value of the tag corresponding to the given property key if available, the default value of the property key otherwise.</returns>\n    public T? TryGetTag<T>(PropertyKey<T> key)\n    {\n        return Tags.Get(key);\n    }\n\n    /// <summary>\n    /// \n    /// </summary>","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorParameters.cs#L165-L201","documentation":"TemplateGeneratorParameters.GetTag<T> throws this KeyNotFoundException when the requested PropertyKey<T> has no value stored in the parameters' Tags PropertyContainer. GetTag is the strict accessor (unlike TryGetTag which returns the default); it is meant for generators that require the tag to be present, so a missing key is surfaced as an exception.","triggerScenarios":"Calling parameters.GetTag(somePropertyKey) where SetTag was never called for that key on this parameters instance (or a copied parameters object didn't carry the tag).","commonSituations":"A custom template generator reading an optional tag that the caller never set; invoking a generator programmatically without the tags the editor normally injects; using a different PropertyKey instance/key than the one that was set; tag set on the original parameters but lost when constructing a new parameters object.","solutions":["Set the tag before running the generator: parameters.SetTag(key, value).","If the tag is optional, switch to TryGetTag(key) or HasTag(key) instead of GetTag.","Verify you are using the exact same PropertyKey<T> instance that was used with SetTag (keys are compared by identity/equality)."],"exampleFix":"// before\nvar outputDir = parameters.GetTag(TemplateGeneratorTags.OutputDir); // may throw\n\n// after\nif (parameters.HasTag(TemplateGeneratorTags.OutputDir))\n{\n    var outputDir = parameters.GetTag(TemplateGeneratorTags.OutputDir);\n}\nelse\n{\n    var outputDir = parameters.TryGetTag(TemplateGeneratorTags.OutputDir); // default\n}","handlingStrategy":"try-catch","validationCode":"if (key != null && !parameters.HasTag(key))\n    throw new InvalidOperationException($\"Required tag '{key.Name}' was not set on template generator parameters.\");","typeGuard":"bool HasRequiredTag<T>(TemplateGeneratorParameters p, PropertyKey<T> key) => p.HasTag(key);","tryCatchPattern":"try { value = parameters.GetTag(key); }\ncatch (KeyNotFoundException)\n{\n    value = parameters.TryGetTag(key); // fall back to default\n    // or log which required tag was missing\n}","preventionTips":["Prefer TryGetTag for optional tags; reserve GetTag for genuinely required ones.","Set all expected tags on the parameters object right after construction.","Reuse the same static PropertyKey instances for set and get — do not create duplicate keys."],"tags":["csharp","key-not-found","templates","tags"],"backgroundTag":"missing-required-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}