Using Late Binding

Late binding is the process of waiting until runtime to bind a method to an object. With late binding, you don't have to specify what object you're going to work with until the code is actually running. This is a useful skill because you can create an instance of an assembly (such as Microsoft Access or Excel) and invoke its methods using late binding.

Here's an example. Say you have a class named Calculator with a method named Addem that adds two integers:

public class Calculator
{
  public virtual long Addem(int x, int y)
  {
    return x + y;
  }
}

Now say you want to invoke the Addem method at runtime using late binding. We'll start by creating a new instance of the Calculator type using the Activator class's CreateInstance method. ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.