Currently I'm try to understand some of aspects regarding programming in C#. Now I'm learning LateBinding
. I understand how to create some simple program like the one below.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Try to do something with late bindings");
Assembly a = null;
try
{
a = Assembly.Load("CarLibrary");
Console.WriteLine("1");
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
if (a == null)
{
CreateUsingLateBinding(a);
}
Console.ReadLine();
}
private static void CreateUsingLateBinding(Assembly asm)
{
try
{
Type sportCar = asm.GetType("CarLibrary.SportCar");
object obj = Activator.CreateInstance(sportCar);
Console.WriteLine("Success");
MethodInfo mi = sportCar.GetMethod("TurboBust");
mi.Invoke(obj, null);
}
catch (Exception)
{ }
}
I also created CarLibrary.dll and put it in one folder. ILDASM screenshot
All works fine. I just have a few questions regarding this topic
- When it's usefull to use this?
- If I use LateBinding is it supposed that I don't know anything about resource that I want to use or I know everything about it (in this case why I just can't write program in normal way, if I know every class and method from this resource)? It's still a little bit confusing to me - try to find answer - result only how to use.