LateBinding - how to use it?

无人久伴 提交于 2020-01-23 17:33:44

问题


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.

回答1:


Well, imagine that you have some child classes

ex Dll A

public class Student : Person { }

Dll B

public class Teacher : Person { }

The Person can be in a common assembly referenced by these dlls and your application, having thus different implementations of a virtual method etc. Using reflection you can load all the classes that inherit from the class Person.

public static IEnumerable<Type> GetSubclassesForType(Type baseClassType)
{
    List<Type> types = new List<Type>();
    foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
    {
       types.AddRange(ass.GetTypes().Where(type => type.IsSubclassOf(baseClassType)));
    }
    return types;
}

public static IEnumerable<Type> GetSubclassesForType(Assembly assembly, Type baseClassType)
{
    return from type in assembly.GetTypes() 
                        where type.IsSubclassOf(baseClassType)    
                        select type;
}

Another use of Late Binding is that it can be used if you want to update your application by only copying the dll that contains some part of your code. This can really help when you want update fast multiple client applications.(Note: you should also cache the results of the reflection after the Late Binding to increase performance)



来源:https://stackoverflow.com/questions/20054897/latebinding-how-to-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!