O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

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

16.5. Dynamically invoking methods in late bound objects

You should have read section 16.3 before embarking on this section.

Late bound objects are objects the type of which we are unsure until runtime. Late binding is more flexible but slower to perform. We use System.Activator's CreateInstance method to create an instance of a class during runtime.

Examine the program below:

 1: using System; 2: using System.Reflection; 3: 4: public class MyClass{ 5: public void DoThis(){ 6: Console.WriteLine("doing this"); 7: } 8: public void DoThat(int a, int b){ 9: Console.WriteLine("doing that " + (a*b)); 10: } 11: } 12: 13: public class TestClass{ 14: public static void Main(){ 15: 16: // can assign typeName to a type known during runtime 17: string typeName ...

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