How to write the following code from VB.NET to C#? -
code is:
public class form1 private sub webbrowser1_documentcompleted(sender object, e webbrowserdocumentcompletedeventargs) handles webbrowser1.documentcompleted dim item htmlelementcollection item = webbrowser1.document.getelementsbytagname("span") dim ht htmlelement each ht in item msgbox(ht.domelement.attributes("class").value.tostring) next end sub end class
in c#, not find .attributes("class").value.tostring()
part.
the reason code works in vb.net , not in c# using option strict off
(perhaps implicitly omitting option strict on
). tells compiler assume there attributes
member in domelement
, object
.
in c# there no equivalent option strict on
in general if use dynamic
type can same thing.
private void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { var item = webbrowser1.document.getelementsbytagname("span"); foreach(htmlelement ht in item) { dynamic element = ht.domelement; messagebox.show(element.attributes["class"].value.tostring()); } }
this allows access properties of types don't know @ design-time long exist @ run-time, similar object
in vb.net option strict off
. careful though, since allows write code recklessly (similar vb :) should add checking , exception handling in case.
Comments
Post a Comment