早期和后期绑定 [英] Early and late binding

查看:270
本文介绍了早期和后期绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想围绕我的头时,早/晚绑定发生在C#。

I'm trying to get my head around when early/late binding occurs in C#.

非虚拟方法总是早期绑定。虚拟方法总是后期绑定的:编译器插入额外的code,以解决在执行时间和类型安全检查,结合实际的方法。所以亚型多态性使用后期绑定。

Non-virtual methods are always early bound. Virtual methods are always late bound: the compiler inserts extra code to resolve the actual method to bind to at execution time and checks for type safety. So subtype polymorphism uses late binding.

使用反射调用方法是后期绑定的一个例子。我们写出code实现,这相对于编译器。 (例如调用COM组件。)

Calling methods using reflection is an example of late binding. We write the code to achieve this as opposed to the compiler. (E.g. calling COM components.)

VB.NET支持隐式后期绑定时,Option Strict为关闭。当它被分配给声明为Object类型的变量的对象被后期绑定。 VB编译器插入code绑定在执行时正确的方法,并赶上无效电话。 C#不支持此功能。

VB.NET supports implicit late binding when Option Strict is off. An object is late bound when it is assigned to a variable declared to be of type Object. The VB compiler inserts code to bind to the right method at execution time and to catch invalid calls. C# does not support this feature.

我在朝着正确的方向?

有关调用代表和通过接口引用调用一个方法是什么?那是早期或后期绑定?

What about calling delegates and calling a method through an interface reference? Is that early or late binding?

推荐答案

一切都在C#早期绑定,除非你经过思考接口。

Everything is early bound in C# unless you go through the Reflection interface.

早期绑定只是意味着目标方法在编译时被发现,和code为创建将调用此。无论是它的虚拟与否(这意味着有一个额外的步骤在通话时间是无关紧要找到它)。如果不存在,编译器的方法将无法编译code。

Early bound just means the target method is found at compile time, and code is created that will call this. Whether its virtual or not (meaning there's an extra step to find it at call time is irrelevant). If the method doesn't exist the compiler will fail to compile the code.

后期绑定意味着目标方法在运行时抬头。通常情况下,方法的文本名称来查找。如果方法不存在,一鼓作气。该计划将崩溃或进入在运行时的一些异常处理机制。

Late bound means the target method is looked up at run time. Often the textual name of the method is used to look it up. If the method isn't there, bang. The program will crash or go into some exception handling scheme at run time.

大多数脚本语言使用后期绑定,并汇编语言使用早期绑定。

Most script languages use late binding, and compiled languages use early binding.

C#(之前的版本4)不迟到不绑定;然而,他们可以使用反射API来做到这一点。该API编译为code,它在运行时通过组件挖掘查找函数名。如果选项严格关闭VB晚期可绑定。

C# (prior to version 4) doesn't late bind; they can however use the reflection API to do it. That API compiles to code that looks up function names by digging through assemblies at run time. VB can late bind if Option Strict is turned off.

绑定通常对性能有影响。由于后期绑定需要在运行时查找,它通常意味着方法调用比早期绑定的方法调用速度较慢。

Binding usually has an effect on performance. Because late binding requires lookups at runtime, it is usually means method calls are slower than early bound method calls.

对于一个正常的函数,编译器可以在内存中运行出来的数字位置。然后,当调用函数时,可产生一个指令调用的函数在这个地址。

For a normal function, the compiler can work out the numeric location of it in memory. Then it when the function is called it can generate an instruction to call the function at this address.

对于有任何虚方法的对象,编译器将生成一个v表。这实质上是一个包含的虚拟方法的地址的数组。具有虚拟方法的每个对象将包含由编译器是V形表的地址生成一个隐藏的部件。当一个虚函数被调用时,编译器将制定出的位置是在V表适当的方法是什么。然后,它会产生code中的对象看v表,并调用虚方法在这个位置。

For an object that has any virtual methods, the compiler will generate a v-table. This is essentially an array that contains the addresses of the virtual methods. Every object that has a virtual method will contain a hidden member generated by the compiler that is the address of the v-table. When a virtual function is called, the compiler will work out what the position is of the appropriate method in the v-table. It will then generate code to look in the objects v-table and call the virtual method at this position.

因此​​,有发生对虚拟函数的查找。这在很大程度上优化,所以它会在运行时发生得很快。

So, there is a lookup that occurs for the virtual function. This is heavily optimized so it will happen very quickly at run-time.

早期绑定


  • 编译器可以工作在哪里调用的函数将在编译的时候。

  • 编译器可以早期(前的任何程序code的运行),保证功能存在,并在运行时调用。

  • 编译器保证了函数调用参数权数,他们是正确的类型。它还检查返回值是正确的类型。

后期绑定


  • ,因为它不是一个简单的偏移计算的查找将需要更长的时间,通常有要进行文本比较。

  • 目标函数可能不存在。

  • 目标函数可能不接受传递给它的参数,并可能有错误类型的返回值。

  • 对于一些实施方案中,目标方法可以在运行时实际改变。因此,该查找可以执行不同的功能。我想,这发生在Ruby语言,可以在程序运行时的对象上定义一个新的方法。后期绑定中,函数调用开始呼吁的方法一个新的覆盖,而不是调用现有的基础方法。

这篇关于早期和后期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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