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

查看:91
本文介绍了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

公共班级学生:人{}

public class Student : Person { }

Dll B

公共课教师:人{}

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天全站免登陆