{"record":{"id":"908121673c45f46e","repo":"cefsharp/CefSharp","slug":"browsersettings-can-only-be-of-type-0-or-null","errorCode":null,"errorMessage":"BrowserSettings can only be of type {0} or null","messagePattern":"BrowserSettings can only be of type (.+?) or null","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.WinForms/ChromiumWebBrowser.cs","lineNumber":131,"sourceCode":"                //created, and changing browserSettings then creating a new handle will\n                //give a subtle different user experience if you aren't expecting it we\n                //return null here even though we still have a reference.\n                if (browserCreated)\n                {\n                    return null;\n                }\n                return browserSettings;\n            }\n            set\n            {\n                if (browserCreated)\n                {\n                    throw new Exception(\"Browser has already been created. BrowserSettings must be \" +\n                                        \"set before the underlying CEF browser is created.\");\n                }\n                if (value != null && !Core.ObjectFactory.BrowserSetingsType.IsAssignableFrom(value.UnWrap().GetType()))\n                {\n                    throw new Exception(string.Format(\"BrowserSettings can only be of type {0} or null\", Core.ObjectFactory.BrowserSetingsType));\n                }\n                browserSettings = value;\n            }\n        }\n        /// <summary>\n        /// Activates browser upon creation, the default value is false. Prior to version 73\n        /// the default behaviour was to activate browser on creation (Equivalent of setting this property to true).\n        /// To restore this behaviour set this value to true immediately after you create the <see cref=\"ChromiumWebBrowser\"/> instance.\n        /// https://github.com/chromiumembedded/cef/issues/1856\n        /// </summary>\n        public bool ActivateBrowserOnCreation { get; set; }\n        /// <summary>\n        /// Gets or sets the request context.\n        /// </summary>\n        /// <value>The request context.</value>\n        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)]\n        public IRequestContext RequestContext\n        {","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.WinForms/ChromiumWebBrowser.cs#L113-L149","documentation":"Thrown by the WinForms ChromiumWebBrowser.BrowserSettings setter when the assigned value, after UnWrap(), is not assignable to CefSharp.Core.BrowserSettings (the only concrete implementation CEF recognizes). CefSharp exposes IBrowserSettings as an interface but the native bridge requires the exact core type; a custom or mock IBrowserSettings whose UnWrap() returns a different type cannot be passed across the boundary. The message interpolates the expected type so you know which concrete class to use.","triggerScenarios":"Assigning a custom IBrowserSettings implementation whose UnWrap() does not return a CefSharp.Core.BrowserSettings instance; passing a mock/stub from a unit test; passing an object from a different CefSharp major version whose internal type differs.","commonSituations":"Unit-testing with a hand-rolled IBrowserSettings; mixing assemblies from different CefSharp versions (the Core type identity differs); accidentally wrapping the settings in another proxy layer.","solutions":["Use `new CefSharp.Core.BrowserSettings()` (or the IBrowserSettings returned by ObjectFactory) rather than a custom implementation.","Ensure all CefSharp.* assemblies (CefSharp, CefSharp.Core, CefSharp.WinForms) are the same version so the BrowserSetingsType identity matches.","If you hold an IBrowserSettings reference, call its UnWrap() and confirm the result is the Core type before assigning.","Pass null instead of a foreign type if you only want defaults."],"exampleFix":"// before\nbrowser.BrowserSettings = myCustomSettings; // myCustomSettings.UnWrap() is a foreign type\n\n// after\nbrowser.BrowserSettings = new CefSharp.Core.BrowserSettings { WebGl = CefState.Disabled };","handlingStrategy":"type-guard","validationCode":"var settings = new BrowserSettings(); // the Core concrete type\nif (settings.UnWrap() is CefSharp.Core.BrowserSettings)\n{\n    browser.BrowserSettings = settings;\n}","typeGuard":"static bool IsValidBrowserSettings(IBrowserSettings value)\n    => value == null || value.UnWrap() is CefSharp.Core.BrowserSettings;","tryCatchPattern":null,"preventionTips":["Only use BrowserSettings produced by CefSharp.Core / ObjectFactory.","Keep all CefSharp.* assemblies at the same version so the Core type identity matches.","Avoid hand-rolled IBrowserSettings implementations except for read-only test doubles."],"tags":["cefwsharp","winforms","type-mismatch","browser-settings"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}