user857521
user857521

Reputation:

Using dynamic type

I'm using a System.Window.Controls Webbrowser (WPF) which is throwing up a few anomolies here and there.

Normally if I want to get access to the webbrowser document in Winforms and click an element I would use

       HtmlDocument document = webNav.webBrowser1.Document;
       document.GetElementById("id_of_element").InvokeMember("Click");

However, in WPF it throws error Cannot implicitly convert type 'object' to 'System.Windows.Forms.HtmlDocument'. An explicit conversion exists (are you missing a cast?).

I can get around this by using

       dynamic document = webNav.webBrowser1.Document;
       document.GetElementById("id_of_element").InvokeMember("Click");

Is there a better/preferred method or is this an acceptable use of the dynamic type? (are there any examples of acceptable use of dynamic type?)

Upvotes: 2

Views: 746

Answers (1)

Chris Laplante
Chris Laplante

Reputation: 29668

Like the error says, you are missing an explicit cast:

 HtmlDocument document = (HtmlDocument)webNav.webBrowser1.Document;

Assuming you have using System.Windows.Forms; at the top of the file (to make the code above shorter).

I knew this because of the line,

An explicit conversion exists (are you missing a cast?)

No use of dynamic is needed in this instance.

Upvotes: 4

Related Questions