程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 動態裝載和使用類型

動態裝載和使用類型

編輯:關於C#
 

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 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  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved