Call the method of the instance class via a string


this link: explains clearly how to invoke a method when you have a method name + type as string variable.

I'm making a c# project with WatiN. All the WatiN methods I use are of the same form:

Examples: *(ById,.ByClass..; so should be connected, but then I could not set it bold :s )

  • browser.**TextField**(Find.By **Id**("name")).**TypeText**("my name");
  • browser.**Span**(Find.By **Class**("main_title")).**Click(**);
  • browser.**Link**(Find.By **Id**("mid_link")).**Click(**);

As you can see, this always consist of 3 methods which are variable. I created a class webElement consisted of the string properties: tag, type, search, action.

Where in example -> tag = "TextField"; type = "Id", search = "name"; action = "TypeText".

To get the web-elements dynamically, I created a WebHandler class where I try to dynamically call the right methods.

So a main class has a list of all the webElement objects and can give the right one at a given time to the WebHandler class. The Webhandler class should now invoke each element dynamically. I use the same invoke method as in given url, so my code to call it is:

class WebHandler:

private IE browser = new IE("");

public void method(WebElement webElement)
     //Get the findBy dynamically | this works
     WatiN.Core.Constraints.Constraint findBy =
                (WatiN.Core.Constraints.Constraint)InvokeMethod("WatiN.Core.Find, WatiN.Core", "By" + webElement.Type, webElement.Search); //where type = "Id" and search = "name"

     //Get the tag (like textfield, link, span) dynamically | this does not work
     Type aType = Type.GetType("WatiN.Core." + webElement.Tag, "WatiN.Core") //how can I set the element variable to this type? aType element -> Does not work
     aType element = (WatiN.Core.TextField)InvokeMethod("this.browser", webElement.Tag, findBy); //tag = TextField

    element.TypeText("a name"); //same problem as above | so should be invoked


  1. How do I invoke method (TextField) of instance class IE (browser) dynamically using his string version "TextField" as variable? Another way of phrasing it would be: How do I get current variable (browser) by using it's string version "browser"?
  2. How do I set type of variable element dynamically? So when webElement.Tag = Textfield then type should be WatiN.Core.TexField element = .. (see code)


  1. Main problem I found is that you can only invoke a method from a type, so not from an instance of that type. Is there a way to do this anyway?

This line

Type aType = Type.GetType("WatiN.Core" + webElement.Tag)

does not have a dot after Core. It seems as if Core is a namespace and should thus be separated from the Tag name.