{"record":{"id":"797d64362fafea9b","repo":"dotnet/aspnetcore","slug":"the-nameof-componentrendermode-field-only-exist","errorCode":null,"errorMessage":"The {nameof(ComponentRenderMode)} field only exists on frames of type {nameof(RenderTreeFrameType.ComponentRenderMode)}.","messagePattern":"The (.+?) field only exists on frames of type (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/RenderTree/RenderTreeFrame.cs","lineNumber":278,"sourceCode":"    // --------------------------------------------------------------------------------\n    // RenderTreeFrameType.ComponentRenderMode\n    // --------------------------------------------------------------------------------\n\n    [FieldOffset(16)] internal IComponentRenderMode ComponentRenderModeField;\n\n    /// <summary>\n    /// If the <see cref=\"FrameType\"/> property equals <see cref=\"RenderTreeFrameType.ComponentRenderMode\"/>,\n    /// gets the specified <see cref=\"IComponentRenderMode\"/>. Otherwise, the value is undefined.\n    /// </summary>\n    public IComponentRenderMode ComponentRenderMode\n    {\n        get\n        {\n            // Normally we don't check the frame type matches, and leave it to the caller to be responsible for only evaluating the correct properties.\n            // However the name \"ComponentRenderMode\" sounds so much like it would be a field on Component frames that we'll explicitly check to avoid mistakes.\n            if (FrameType != RenderTreeFrameType.ComponentRenderMode)\n            {\n                throw new InvalidOperationException($\"The {nameof(ComponentRenderMode)} field only exists on frames of type {nameof(RenderTreeFrameType.ComponentRenderMode)}.\");\n            }\n\n            return ComponentRenderModeField;\n        }\n    }\n\n    // --------------------------------------------------------------------------------\n    // RenderTreeFrameType.NamedEvent\n    // --------------------------------------------------------------------------------\n\n    [FieldOffset(16)] internal string NamedEventTypeField;\n    [FieldOffset(24)] internal string NamedEventAssignedNameField;\n\n    /// <summary>\n    /// If the <see cref=\"FrameType\"/> property equals <see cref=\"RenderTreeFrameType.NamedEvent\"/>,\n    /// gets the event type. Otherwise, the value is undefined.\n    /// </summary>\n    public string NamedEventType => NamedEventTypeField;","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/RenderTree/RenderTreeFrame.cs#L260-L296","documentation":"The RenderTreeFrame.ComponentRenderMode property was accessed on a frame whose FrameType is not ComponentRenderMode. Unlike most frame field accessors, this one explicitly validates because the name is easily confused with the Component frame type. Thrown from the getter of RenderTreeFrame.ComponentRenderMode.","triggerScenarios":"Reading frame.ComponentRenderMode on a frame obtained via an index/iteration without first checking frame.FrameType == RenderTreeFrameType.ComponentRenderMode. Tree-walking/diagnostics code that assumes all 'component-ish' frames carry a render mode.","commonSituations":"Custom render tree visitors/analyzers. Incorrectly porting code that read Component fields and assuming ComponentRenderMode lives on Component frames. Reflecting over frames in a test helper.","solutions":["Guard access with `if (frame.FrameType == RenderTreeFrameType.ComponentRenderMode) ...` before reading the property.","Restructure the tree walk to dispatch on FrameType first.","If you actually need the render mode declared on a Component frame, read the ComponentRenderMode child frame, not a field on the Component frame."],"exampleFix":"// before\nvar mode = frame.ComponentRenderMode;\n\n// after\nIComponentRenderMode? mode = frame.FrameType == RenderTreeFrameType.ComponentRenderMode\n    ? frame.ComponentRenderMode\n    : null;","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static bool IsComponentRenderModeFrame(in RenderTreeFrame f)\n    => f.FrameType == RenderTreeFrameType.ComponentRenderMode;","tryCatchPattern":null,"preventionTips":["Always dispatch on FrameType before reading frame fields in tree-walking code.","Treat ComponentRenderMode as a child frame of Component, not a field on Component.","Add a regression test that walks mixed frame types and only reads matching fields."],"tags":["blazor","render-tree","render-frame","render-mode","api-misuse"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}