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

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

问题描述

当 C# 中出现早/晚绑定时,我正在努力解决问题.

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

非虚方法总是提前绑定的.虚拟方法总是后期绑定:编译器插入额外的代码来解析在执行时绑定到的实际方法并检查类型安全.所以亚型多态使用后期绑定.

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.

使用反射调用方法是后期绑定的一个例子.我们编写代码来实现这一点,而不是编译器.(例如调用 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.)

当 Option Strict 关闭时,VB.NET 支持隐式后期绑定.当一个对象被分配给一个声明为 Object 类型的变量时,它就是后期绑定的.VB 编译器插入代码以在执行时绑定到正确的方法并捕获无效调用.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.

早期绑定仅意味着在编译时找到目标方法,并创建将调用此方法的代码.它是否是虚拟的(意味着在调用时需要一个额外的步骤来找到它是无关紧要的).如果该方法不存在,编译器将无法编译代码.

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 编译为通过在运行时挖掘程序集来查找函数名称的代码.如果 Option Strict 关闭,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-table.这本质上是一个包含虚拟方法地址的数组.每个具有虚方法的对象都将包含一个由编译器生成的隐藏成员,即 v-table 的地址.当调用虚函数时,编译器会计算出合适方法在 v-table 中的位置.然后它会生成代码来查看对象的 v-table 并在这个位置调用虚方法.

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.

提前绑定

  • 编译器可以在编译时确定被调用函数的位置.
  • 编译器可以尽早(在任何程序代码运行之前)保证该函数将存在并可在运行时调用.
  • 编译器保证函数采用正确数量的参数并且它们是正确的类型.它还检查返回值的类型是否正确.

后期绑定

  • 查找时间会更长,因为它不是简单的偏移量计算,通常需要进行文本比较.
  • 目标函数可能不存在.
  • 目标函数可能不接受传递给它的参数,并且可能具有错误类型的返回值.
  • 在某些实现中,目标方法实际上可以在运行时更改.因此,查找可能会执行不同的功能.我认为这发生在 Ruby 语言中,您可以在程序运行时在对象上定义一个新方法.后期绑定允许函数调用开始为方法调用新的覆盖,而不是调用现有的基方法.

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

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