{"record":{"id":"d13404a6780e5dfc","repo":"PrismLibrary/Prism","slug":"the-object-must-be-of-type-0-in-order-to-use-the-current","errorCode":null,"errorMessage":"The object must be of type '{0}' in order to use the current region adapter.","messagePattern":"The object must be of type '(.+?)' in order to use the current region adapter\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/Adapters/RegionAdapterBase.cs","lineNumber":132,"sourceCode":"    /// </summary>\n    /// <param name=\"region\">The new region being used.</param>\n    /// <param name=\"regionTarget\">The object to adapt.</param>\n    protected abstract void Adapt(IRegion region, T regionTarget);\n\n    /// <summary>\n    /// Template method to create a new instance of <see cref=\"IRegion\"/>\n    /// that will be used to adapt the object.\n    /// </summary>\n    /// <returns>A new instance of <see cref=\"IRegion\"/>.</returns>\n    protected abstract IRegion CreateRegion(IContainerProvider container);\n\n    private static T GetCastedObject(object regionTarget)\n    {\n        if (regionTarget == null)\n            throw new ArgumentNullException(nameof(regionTarget));\n\n        if (regionTarget is not T castedObject)\n            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.AdapterInvalidTypeException, typeof(T).Name));\n\n        return castedObject;\n    }\n\n    private static void SetObservableRegionOnHostingControl(IRegion region, T regionTarget)\n    {\n        if (regionTarget is VisualElement targetElement)\n        {\n            // Set the region as a dependency property on the control hosting the region\n            // Because we are using an observable region, the hosting control can detect that the\n            // region has actually been created. This is an ideal moment to hook up custom behaviors\n            Xaml.RegionManager.GetObservableRegion(targetElement).Value = region;\n        }\n    }\n}\n","sourceCodeStart":114,"sourceCodeEnd":148,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/Adapters/RegionAdapterBase.cs#L114-L148","documentation":"RegionAdapterBase<T>.GetCastedObject throws InvalidOperationException (Resources.AdapterInvalidTypeException) when the regionTarget object is not an instance of the adapter's supported control type T. Each adapter only handles one control type; this guard rejects mismatched targets.","triggerScenarios":"Registering a mapping whose adapter's T does not match the control being adapted, or manually calling adapter.Initialize(controlOfWrongType, name) — e.g. passing a ContentView to ScrollViewRegionAdapter. Also occurs when RegisterMapping<TControl, TAdapter> declares TControl inconsistent with the adapter's supported type.","commonSituations":"Custom IRegionAdapter implementations written for one control type but registered for another; copy-pasted region registration code; framework upgrades where control hierarchies changed so GetType() no longer matches.","solutions":["Ensure the control marked as a region matches the adapter type registered in ConfigureRegionAdapterMappings","Register a custom adapter for the unsupported control type and map the correct TControl","Fix custom adapters so the generic parameter T matches the control type they are mapped to"],"exampleFix":"// before\nmappings.RegisterMapping<ScrollView, ContentViewRegionAdapter>(); // adapter targets ContentView\n\n// after\nmappings.RegisterMapping<ScrollView, ScrollViewRegionAdapter>();","handlingStrategy":"type-guard","validationCode":"if (regionTarget is not ScrollView) throw new InvalidOperationException(\"Control type does not match the registered region adapter\");","typeGuard":"bool MatchesAdapter<T>(object c) where T : class => c is T;","tryCatchPattern":"try { adapter.Initialize(target, name); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"region adapter\")) { /* wrong control type for this adapter */ }","preventionTips":["Match RegisterMapping<TControl, TAdapter> generic parameters to the adapter's supported type","Write a dedicated adapter per control type instead of reusing across types","Re-check mappings after MAUI/Prism upgrades if control types changed"],"tags":["prism","maui","regions","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}