O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Invoke a Method by Name

Problem

You want to invoke a method or set a property that’s named in a string.

Solution

Use the Type.InvokeMember method.

Discussion

The Type class provides an InvokeMember method that’s similar to the CallByName function in Visual Basic 6. It requires the object; the name of the field, property, or method (as a string); a flag that indicates whether the string corresponds to a field, property, or method; and an array of objects for any required parameters. For example, you can call a method with no arguments using this syntax:

Dim MyObject As New MyClass() Dim TypeInfo As Type = MyObject.GetType() ' Call Refresh() on MyObject. Dim Args() As Object = {} TypeInfo.InvokeMember("Refresh", BindingFlags.Public Or _ BindingFlags.InvokeMethod, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required