Understanding late binding
When you reference an assembly at compile time, the compiler has full access to the types available in that assembly. This is called early binding. However, if an assembly is only loaded at runtime, the compiler has no access to the content of that assembly. This is called late binding and is key to building extensible applications. Using late binding, you can not only load and query assemblies but also execute code. We will see that in the following examples.
Let's imagine the Engine
class, shown earlier, is available in an assembly called EngineLib
. This can be loaded with either Assembly.Load()
or Assembly.LoadFrom()
. Once loaded, we can get information about the Engine
type using Assembly.GetType()
and the class methods of Type
. However, using Assembly.CreateInstance()
, we can instantiate an object of the class:
var assembly = Assembly.LoadFrom("EngineLib.dll"); if (assembly != null) { var type = assembly.GetType...