{"record":{"id":"f0648a5b77b00bba","repo":"dotnet/aspnetcore","slug":"the-componentrendermode-field-only-exists-on-frame","errorCode":null,"errorMessage":"The ComponentRenderMode field only exists on frames of type ComponentRenderMode.","messagePattern":"The ComponentRenderMode field only exists on frames of type ComponentRenderMode\\.","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/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/RenderTree/RenderTreeFrame.cs#L260-L296","documentation":"RenderTreeFrame is a struct with explicit memory layout where multiple frame types share the same field offset (FieldOffset(16)). The ComponentRenderMode property explicitly validates that FrameType equals ComponentRenderMode before returning the field, unlike most other properties that trust the caller. This prevents reading meaningless data when the offset is occupied by a different frame type's data.","triggerScenarios":"Calling frame.ComponentRenderMode on a RenderTreeFrame whose FrameType is not RenderTreeFrameType.ComponentRenderMode. This is an internal API misuse, typically in custom RenderTreeBuilder traversal code or in tests that enumerate frames and access properties without checking FrameType first.","commonSituations":"Writing custom code that iterates over RenderTreeBuilder frames and accesses properties without first checking FrameType; unit tests inspecting render trees; incorrectly typed frame access in low-level rendering extensions.","solutions":["Always check frame.FrameType == RenderTreeFrameType.ComponentRenderMode before accessing frame.ComponentRenderMode.","Use a switch on FrameType to dispatch to the correct property accessor for each frame type.","If iterating frames in a test, use the RenderTreeFrame collection extension methods that handle type dispatching.","Avoid reading frame properties directly unless you have verified the FrameType."],"exampleFix":"// before\nforeach (ref var frame in builder.GetFrames())\n{\n    var mode = frame.ComponentRenderMode; // throws if not ComponentRenderMode\n}\n\n// after\nforeach (ref var frame in builder.GetFrames())\n{\n    if (frame.FrameType == RenderTreeFrameType.ComponentRenderMode)\n    {\n        var mode = frame.ComponentRenderMode;\n    }\n}","handlingStrategy":"type-guard","validationCode":"// Always check FrameType before accessing ComponentRenderMode\nstatic IComponentRenderMode? SafeGetComponentRenderMode(ref RenderTreeFrame frame)\n    => frame.FrameType == RenderTreeFrameType.ComponentRenderMode\n        ? frame.ComponentRenderMode\n        : null;","typeGuard":"static bool IsComponentRenderModeFrame(ref RenderTreeFrame frame)\n    => frame.FrameType == RenderTreeFrameType.ComponentRenderMode;","tryCatchPattern":null,"preventionTips":["Always check frame.FrameType before accessing any frame-type-specific property.","Use switch expressions on FrameType to dispatch property access.","Write unit tests for custom RenderTreeBuilder traversals that cover all frame types.","Prefer using the RenderTreeBuilder collection extension methods over direct property access."],"tags":["blazor","rendertreeframe","rendertreebuilder","internal","validation"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}