{"record":{"id":"6682626666b338db","repo":"PrismLibrary/Prism","slug":"resources-adapterinvalidtypeexception","errorCode":null,"errorMessage":"Resources.AdapterInvalidTypeException","messagePattern":"Resources\\.AdapterInvalidTypeException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf/Prism.Wpf/Navigation/Regions/RegionAdapterBase.cs","lineNumber":134,"sourceCode":"        /// <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();\n\n        private static T GetCastedObject(object regionTarget)\n        {\n            if (regionTarget == null)\n                throw new ArgumentNullException(nameof(regionTarget));\n\n            T castedObject = regionTarget as T;\n\n            if (castedObject == null)\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            DependencyObject targetElement = regionTarget as DependencyObject;\n\n            if (targetElement != null)\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                RegionManager.GetObservableRegion(targetElement).Value = region;\n            }\n        }\n    }\n}","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Wpf/Prism.Wpf/Navigation/Regions/RegionAdapterBase.cs#L116-L152","documentation":"RegionAdapterBase<T>.GetCastedObject casts the regionTarget control to the adapter's generic type T; if the control does not implement/inherit T, it throws InvalidOperationException with AdapterInvalidTypeException naming typeof(T).Name. This guards that a region adapter is only applied to control types it supports.","triggerScenarios":"Registering a RegionAdapterMappings entry for a control type (or derived type) that the adapter does not support — e.g. mapping a custom ContentControl-derived adapter to a control that isn't a ContentControl, then initializing a region on that control.","commonSituations":"Custom controls that look like the base type but derive from a different branch; adapting a third-party control with an adapter built for stock Prism controls; mapping changes after upgrading Prism where base-type expectations changed.","solutions":["Make the control derive from (or implement) the adapter's required type T, e.g. derive from ContentControl for ContentControlRegionAdapter.","Register the adapter only for the correct type in RegionAdapterMappings: RegisterMapping(typeof(CorrectControl), adapter).","Write a dedicated IRegionAdapter for the custom control type instead of reusing an incompatible one."],"exampleFix":"// before\ncontainerRegistry.RegisterAdapterForSpecificType<StackPanel, MyContentControlAdapter>();\n// after\nclass MyPanel : ContentControl { ... }\ncontainerRegistry.RegisterAdapterForSpecificType<MyPanel, MyContentControlAdapter>();","handlingStrategy":"validation","validationCode":"if (!(regionTarget is ContentControl))\n    throw new InvalidOperationException(\"This adapter requires a ContentControl-derived target.\");","typeGuard":"static bool SupportsAdapter<T>(object control) where T : class => control is T;","tryCatchPattern":"try\n{\n    adapter.Initialize(control, regionName);\n}\ncatch (InvalidOperationException ex)\n{\n    logger.LogError(ex, \"Control type not supported by this region adapter.\");\n}","preventionTips":["Match adapter mappings to the control's actual inheritance chain.","Derive custom controls from the base type your adapter expects.","Test region initialization for every custom control type at startup."],"tags":["wpf","prism","region-adapter","type-mismatch","invalid-operation"],"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"}