mshtml / interface / COM Generic Casting example

ever tried something like this:
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:

this OC goes to all interfaces based classes like most of the COM object we have in C#


Popular posts from this blog

c# Service Play Sound with NAudio example by Moshe

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

JS/JQ simulate Enter event