动态装载和使用类型(1)
点击次数:17 次 发布日期:2008-11-26 12:32:47 作者:源代码网
|
源代码网推荐 源代码网推荐 在以下代码例子中, 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 Hello World string. 源代码网推荐 private const String m_helloWorld = "Hello World"; 源代码网推荐 // Default public constructor. 源代码网推荐 public HelloWorld() { 源代码网推荐 } 源代码网推荐 // Print "Hello World" plus the passed text. 源代码网推荐 public void PrintHello(String txt) { 源代码网推荐 // Output to the Console. 源代码网推荐 Console.WriteLine(m_helloWorld + " " + txt); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 // Illustrates reflection"s late binding functionality. 源代码网推荐 // Calls the PrintHello method on a dynamically loaded 源代码网推荐 // and created instance of the HelloWorld class. 源代码网推荐 using System; 源代码网推荐 using System.Reflection; 源代码网推荐 public class CSharpLateHello { 源代码网推荐 public static void Main() { 源代码网推荐 // Load the assembly to use. 源代码网推荐 Assembly assem = Assembly.Load("HelloWorld"); 源代码网推荐 // Get the type to use from the assembly. 源代码网推荐 Type helloType = assem.GetType("HelloWorld"); 源代码网推荐 // Get the method to call from the type. 源代码网推荐 MethodInfo printMethod = helloType.GetMethod("PrintHello"); 源代码网推荐 // Create an instance of the HelloWorld class. 源代码网推荐 Object obj = Activator.CreateInstance(helloType); 源代码网推荐 // Create the args array. 源代码网推荐 Object[] args = new Object[1]; 源代码网推荐 // Set the arguments. 源代码网推荐 args[0] = "From CSharp Late Bound"; 源代码网推荐 // Invoke the PrintHello method. 源代码网推荐 printMethod.Invoke(obj, args); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 InvokeMember 与 CreateInstance 源代码网推荐 可以使用Type.InvokeMember来调用某类型成员。各种类的CreateInstance 方法,例如System.Activator 和 System.Reflection.Assembly,是InvokeMember的专用形式,用于生成某类型新的实例。Binder类在这些方法中,被用于重载解析和参数转换。 源代码网推荐 源代码网推荐 以下例子中的代码显示了三种可能的参数转换及成员选择的组合。在Case1中, 不需要参数转换或成员选择。在Case 2中,只需要成员选择。在Case3中, 只需要参数转换。 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
