I want to achieve late binding for the following lines of code which are using early binding. How can I achieve it in C#?
When I removed Microsoft.office.interop.wor
Historically, late binding was possible (and still is!) with reflection:
object word = ...;
object[] args = new object [] { oMissing, oMissing, oMissing };
word.GetType().InvokeMember( "Quit",
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
null, word, args );
With the introduction of dynamics in C#4, late binding is just a matter of switching to the dynamic binder:
dynamic word = ...;
word.Quit();
It is really as simple as that, you don't even have to pass these optional parameters to the Quit
method. The only drawback is that there is no intellisense on the dynamically typed code.
More on that here in the Dino's article:
http://msdn.microsoft.com/en-us/magazine/ff714583.aspx