LateBinding - 如何使用它? [英] LateBinding - how to use it?

查看:31
本文介绍了LateBinding - 如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试了解有关 C# 编程的一些方面.现在我正在学习 LateBinding.我了解如何创建一些简单的程序,如下所示.

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)
        { }
    }

我还创建了 CarLibrary.dll 并将其放在一个文件夹中.ILDASM截图

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

  • 什么时候使用它有用?
  • 如果我使用 LateBinding 是不是我对我想使用的资源一无所知,或者我知道它的一切(在这种情况下,为什么我不能以正常方式编写程序,如果我知道每个此资源中的类和方法)?对我来说仍然有点困惑 - 尝试找到答案 - 结果只是如何使用.

推荐答案

好吧,假设您有一些子类

Well, imagine that you have some child classes

ex DLL A

公开课学生:Person { }

public class Student : Person { }

DLL B

公开课老师:Person { }

public class Teacher : Person { }

Person 可以位于这些 dll 和您的应用程序引用的公共程序集中,因此具有虚拟方法的不同实现等.使用反射可以加载从类 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;
}

后期绑定的另一个用途是,如果您只想通过复制包含部分代码的 dll 来更新您的应用程序,则可以使用它.当您想要快速更新多个客户端应用程序时,这真的很有帮助.(注意:您还应该缓存后期绑定后的反射结果以提高性能)

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)

这篇关于LateBinding - 如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆