日期:2009-07-18  浏览次数:20498 次

Reflection提供诸如Microsoft Visual Basic.NET和JScript语言编译器使用的底层结构来实施隐性后绑定。绑定是定位与某一特定类型相对应的声明的过程。当这个过程发生在运行的时候,而不是编译的时候,它被称为后绑定。Visual Basic.NET使你可以在你的代码中使用隐性后绑定;VisualBasic.NET编译器调用helper 方法,使用Reflection获得对象类型。传递给helper 方法的参数 使适当的方法可以在运行时被调用。这些参数是调用方法(对象)的实例,被调用方法的名字(字符串),及传递给被调用方法的参数。(一个对象数组)。

在以下代码例子中, Visual Basic.NET编译器通过Reflection隐性地 来对一在编译时不知类型的对象调用方法。HelloWorld 类有一种 PrintHello 方法,可以打印出 "Hello World" 及传递给PrintHello 方法的一些文本。本例中PrintHello 方法 调用实际上是Type. InvokeMember ; Visual Basic 代码 允许PrintHello 方法被调用,仿佛 对象的类型 (helloObj)在编译时就已经知道了(前期绑定),而不是在运行时(后绑定)。
[Visual Basic]
Imports System
Module Hello
Sub Main()
' Set up variable.
Dim helloObj As Object
' Create the object.
helloObj = new HelloWorld()
' Invoke the print method as if it was early bound
' even though it's really late bound.
helloObj.PrintHello("Visual Basic Late Bound")
End Sub
End Module 

自定义绑定
Reflection除了可以隐性地被编译器用于后绑定,也可以在代码中显示使用,来完成后绑定。

common language runtime 支持多种编程语言,这些语言的绑定规则互不相同。在前绑定的情况下,代码生成器能完全控制绑定。然而,在使用Reflection的后绑定中,绑定必须由自定义绑定控制。Binder类提供成员选择与调用的自定义控制。 

使用自定义绑定, 您可以在运行时装载assembly,获得assembly中关于类型的信息,指明您索要的类型,并且调用方法,访问字段,或类型的属性。如果在编译时您不知道对象的类型,该技术就显得格外有用,比如,当对象类型依赖于用户输入时。以下例子中的代码显示了在HelloWorld.dll assembly 中,被动态使用Reflection调用的方法,第一个在Visual Basic.NET,第二个在C#中。
[Visual Basic]
' This class is deployed as an assembly consisting Hello World string.
Private m_helloWorld As String = "HelloWorld"
' Default public constructor.
Public Sub New()

End Sub 'New

' Print "Hello World" plus thepassed text.
Public Sub PrintHello(txt As String)
' Output to the Console.
Console.WriteLine((m_helloWorld & "" & txt))
End Sub
End Class

Imports System
Imports System.Reflection
Module VisualBasicLateHello
Sub Main()
' Set up the variables.
Dim assem as System.Reflection.Assembly
Dim obj as Object
Dim helloType as Type
Dim printMethod as MethodInfo
' Load the assembly to use.
assem = System.Reflection.Assembly.Load("HelloWorld")
' Get the type to use from the assembly.
helloType = assem.GetType("HelloWorld")
' Get the method to use from the type.
printMethod = helloType.GetMethod("PrintHello")
' Create an instance of the type.
obj = Activator.CreateInstance(helloType)
' Create an array to hold the arguments.
Dim args(1) as Object
' Set the arguments.
args(0) = "From Visual Basic Late Bound"
' Invoke the method.
printMethod.Invoke(obj, args)
End Sub
End Module
 

以下为C# 版:
[C#]
// This class is deployed as an assembly consisting of one DLL,
// called HelloWorld.dll.
using System;
public class HelloWorld {
// Constant Hel