{"record":{"id":"a793e8b2862377d8","repo":"dotnet/wpf","slug":"sr-needtobecomvisible","errorCode":null,"errorMessage":"SR.NeedToBeComVisible","messagePattern":"SR\\.NeedToBeComVisible","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs","lineNumber":432,"sourceCode":"        public object ObjectForScripting\n        {\n            get\n            {\n                VerifyAccess();\n\n                return _objectForScripting;\n            }\n            set\n            {\n                VerifyAccess();\n\n                if (value != null)\n                {\n                    Type t = value.GetType();\n\n                    if (!Marshal.IsTypeVisibleFromCom(t))\n                    {\n                        throw new ArgumentException(SR.NeedToBeComVisible);\n                    }\n                }\n\n                _objectForScripting = value;\n                _hostingAdaptor.ObjectForScripting = value;\n            }\n        }\n\n        /// <summary>\n        /// The HtmlDocument for page hosted in the html page.  If no page is loaded, it returns null.\n        /// </summary>\n        public object Document\n        {\n            get\n            {\n                VerifyAccess();\n\n","sourceCodeStart":414,"sourceCodeEnd":450,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs#L414-L450","documentation":"Setting WebBrowser.ObjectForScripting throws ArgumentException (SR.NeedToBeComVisible) when the object's type is not visible from COM. The object is exposed to page JavaScript through COM interop, so its class must be marked [ComVisible(true)].","triggerScenarios":"Assigning an instance of a plain class without [ComVisible(true)] to webBrowser.ObjectForScripting; assigning anonymous or generic types; the type becoming COM-invisible after assembly attributes changed.","commonSituations":"Forgetting the [ComVisible(true)] attribute on the scripting bridge class; passing a lambda-created or internal type; .NET version/registration changes affecting COM visibility defaults.","solutions":["Decorate the class with [ComVisible(true)] (public class) before assigning it.","Use a dedicated public script-bridge class instead of anonymous/internal types.","Verify with Marshal.IsTypeVisibleFromCom(obj.GetType()) before assignment.","Ensure the assembly/type is not blocked by visibility settings (internal nested classes need to be made public)."],"exampleFix":"// before\nwebBrowser.ObjectForScripting = new { Notify = (Action<string>)(s => ...) };\n// after\n[ComVisible(true)]\npublic class ScriptBridge { public void Notify(string s) { ... } }\n\nwebBrowser.ObjectForScripting = new ScriptBridge();","handlingStrategy":"validation","validationCode":"if (!Marshal.IsTypeVisibleFromCom(obj.GetType())) throw new ArgumentException(\"ObjectForScripting must be ComVisible\");","typeGuard":"bool IsComVisible(object o) => o != null && Marshal.IsTypeVisibleFromCom(o.GetType());","tryCatchPattern":"try { webBrowser.ObjectForScripting = bridge; } catch (ArgumentException) { /* apply [ComVisible(true)] and re-init */ }","preventionTips":["Always mark script bridge classes [ComVisible(true)] public","Test COM exposure with Marshal.IsTypeVisibleFromCom in unit tests"],"tags":["wpf","webbrowser","com","comvisible","interop"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}