Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser claims "object type is not visible to COM" on ObjectForScripting

Tags:

javascript

c#

wpf

I'm trying to set up an interop between C# and JavaScript using the WPF WebBrowser control. The C#->JavaScript calls work great so far, but I can't get the JavaScript->C# operational.

I've created a class for the object:

[ComVisible(true)]
class BrowserClient
{
    private MainWindow owner;

    public string id = "browser-client";

    public BrowserClient(MainWindow owner)
    {
        this.owner = owner;
    }

    public void sendMessage(string date)
    {
        owner.OnReceiveMessage(date);
    }
}

Note that the ComVisible attribute.

But when I set the ObjectForScripting property in the Window_Loaded event:

webBrowser.ObjectForScripting = new BrowserClient(this);

I get the following (very confusing) exception:

An unhandled exception of type 'System.ArgumentException' occurred in PresentationFramework.dll

Additional information: The object type is not visible to COM. You need to set ComVisibleAttribute attribute to True.

Needless to say, I'm pretty baffled. What is happening here?

Update: It might be worth mentioning that I'm using .NET 4.0 on Windows 8.1 with Visual Studio 2013 Express for Desktop.

like image 850
DallonF Avatar asked Dec 24 '13 17:12

DallonF


1 Answers

I think, I know your problem - you need to make your class public

like image 185
T.S. Avatar answered Sep 20 '22 08:09

T.S.