mshtml / interface / COM Generic Casting example
ever tried something like this:
PrintAllElements<HTMLAnchorElement>();
and got all the elements where u have:
public static bool IsTypeOf<T>(object o)
{ return (o is T); }
private void PrintAllElements<T>()
{
HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument;
foreach (IHTMLElement elem in doc.all)
{
if (IsTypeOf<T>(elem))
{ //do stuff }
}
}
i mean IsTypeOf always true?
thats cuz all the HTML classes inherit the nice IHTMLElement so any IHTMLElement is T where T is HTMLxxxxxElement(Class).
but the solution is so very simple - send the interface as T like IHTMLAnchorElement so this works great with the same code:
PrintAllElements<IHTMLAnchorElement>();
this OC goes to all interfaces based classes like most of the COM object we have in C#
PrintAllElements<HTMLAnchorElement>();
and got all the elements where u have:
public static bool IsTypeOf<T>(object o)
{ return (o is T); }
private void PrintAllElements<T>()
{
HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument;
foreach (IHTMLElement elem in doc.all)
{
if (IsTypeOf<T>(elem))
{ //do stuff }
}
}
i mean IsTypeOf always true?
thats cuz all the HTML classes inherit the nice IHTMLElement so any IHTMLElement is T where T is HTMLxxxxxElement(Class).
but the solution is so very simple - send the interface as T like IHTMLAnchorElement so this works great with the same code:
PrintAllElements<IHTMLAnchorElement>();
this OC goes to all interfaces based classes like most of the COM object we have in C#
Comments
Post a Comment