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#

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

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