DynamicMethod 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
創造出動態的方法。
多載
| 名稱 | Description |
|---|---|
| DynamicMethod(String, Type, Type[]) |
初始化一個匿名託管的動態方法,指定方法名稱、回傳類型及參數類型。 |
| DynamicMethod(String, Type, Type[], Boolean) |
初始化一個匿名託管的動態方法,指定方法名稱、回傳類型、參數類型,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。 |
| DynamicMethod(String, Type, Type[], Module) |
建立一個動態方法,該方法對模組為全域,指定方法名稱、回傳類型、參數類型及模組。 |
| DynamicMethod(String, Type, Type[], Type) |
建立一個動態方法,指定方法名稱、回傳類型、參數類型,以及動態方法邏輯關聯的類型。 |
| DynamicMethod(String, Type, Type[], Module, Boolean) |
建立一個全域於模組的動態方法,指定方法名稱、回傳類型、參數類型、模組,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。 |
| DynamicMethod(String, Type, Type[], Type, Boolean) |
建立一個動態方法,指定方法名稱、回傳類型、參數類型、動態方法邏輯關聯的類型,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的即時(JIT)可見性檢查。 |
| DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Module, Boolean) |
建立一個全域於模組的動態方法,指定方法名稱、屬性、呼叫慣例、回傳型別、參數類型、模組,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。 |
| DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type, Boolean) |
建立動態方法,指定方法名稱、屬性、呼叫慣例、回傳型別、參數型別、動態方法邏輯關聯的型別,以及是否應跳過動態方法Microsoft中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。 |
DynamicMethod(String, Type, Type[])
初始化一個匿名託管的動態方法,指定方法名稱、回傳類型及參數類型。
public:
DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public DynamicMethod(string name, Type returnType, Type[] parameterTypes);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type())
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
例外狀況
其中 parameterTypes 一個元素是 null 或 Void。
name 是 null。
.NET Framework 與 2.1 .NET Core 版本:returnType 是 IsByRef 回傳 true 的類型。
備註
由此建構子所建立的動態方法,與匿名組件相關聯,而非現有的型別或模組。 匿名組合語言僅存在於為動態方法提供沙盒環境,也就是將它們與其他程式碼隔離。 此環境使得部分信任的程式碼能安全地發出並執行動態方法。
此建構器規定,動態方法的 Microsoft 中介語言(MSIL)將強制執行即時(JIT)可視性檢查。 也就是說,動態方法中的程式碼可以存取公開類別的公開方法。 若方法嘗試存取類型或成員為 private、protected 或 internal(在 Visual Basic 中為 Friend),則會拋出例外。 若要建立一個動態方法,且能限制跳過 JIT 可見性檢查,請使用建構子。DynamicMethod(String, Type, Type[], Boolean)
當建立匿名託管的動態方法時,會包含該發射組件的呼叫堆疊。 當方法被呼叫時,會使用發射組件的權限,而非實際呼叫者的權限。 因此,動態方法無法執行高於發出它的組合語言的權限,即使它被傳遞並執行給擁有較高信任等級的組裝。
此建構子指定方法屬性 MethodAttributes.Public 與 MethodAttributes.Static,以及呼叫慣例 CallingConventions.Standard。
Note
此建構器於 .NET Framework 3.5 或更新版本中引入。
另請參閱
適用於
DynamicMethod(String, Type, Type[], Boolean)
初始化一個匿名託管的動態方法,指定方法名稱、回傳類型、參數類型,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。
public:
DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, bool restrictedSkipVisibility);
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), restrictedSkipVisibility As Boolean)
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
- restrictedSkipVisibility
- Boolean
true跳過動態方法中MSIL存取的型別與成員的JIT可見性檢查,但有以下限制:包含這些型別與成員的組件的信任等級必須等於或低於發出動態方法的呼叫堆疊的信任等級;否則,。 false
例外狀況
其中 parameterTypes 一個元素是 null 或 Void。
name 是 null。
.NET Framework 與 2.1 .NET Core 版本:returnType 是 IsByRef 回傳 true 的類型。
備註
由此建構子所建立的動態方法,與匿名組件相關聯,而非現有的型別或模組。 匿名組合語言僅存在於為動態方法提供沙盒環境,也就是將它們與其他程式碼隔離。 此環境使得部分信任的程式碼能安全地發出並執行動態方法。
匿名託管的動態方法無法自動存取任何類型或成員,這些類型private、protected 或 internal(Visual Basic 中稱為Friend)。 這與與現有型別或模組相關的動態方法不同,後者可存取其相關範圍內的隱藏成員。
請指定 true 給 restrictedSkipVisibility,如果你的動態方法必須存取 private、protected 或 internal 的類型或成員。 這使動態方法對這些成員的存取受限制。 也就是說,只有在符合以下條件時,才能存取這些成員:
目標成員屬於一個擁有等於或低於發出動態方法的呼叫堆疊的信任等級的集合體。
發出動態方法的呼叫堆疊會被賦予標記 ReflectionPermission 和旗標 ReflectionPermissionFlag.RestrictedMemberAccess。 當程式碼以完全信任執行時,這點總是成立的。 對於部分可信的程式碼,只有當主機明確授予權限時才成立。
Important
若未授予權限,則在呼叫或動態方法被呼叫時拋出 CreateDelegate 安全異常,而非此建構子被呼叫時。 發射動態方法不需要特殊權限。
例如,一個動態restrictedSkipVisibilitytrue方法設定為 ,若呼叫堆疊已獲得限制成員存取權,則可存取呼叫堆疊中任一組裝的私人成員。 若動態方法在呼叫堆疊中使用部分受信任的程式碼,則無法存取 .NET Framework 組合語言中某類型的私有成員,因為此類組合是完全受信任的。
若 restrictedSkipVisibility 為 false,則會強制執行 JIT 可視性檢查。 動態方法中的程式碼可存取公開類別的公開方法,若嘗試存取屬於 private、 protected、 或 internal的類型或成員,則會拋出例外。
當建立匿名託管的動態方法時,會包含該發射組件的呼叫堆疊。 當方法被呼叫時,會使用發射呼叫堆疊的權限,而非實際呼叫者的權限。 因此,動態方法無法執行高於發出它的組合語言的權限,即使它被傳遞並執行給擁有較高信任等級的組裝。
此建構子指定方法屬性 MethodAttributes.Public 與 MethodAttributes.Static,以及呼叫慣例 CallingConventions.Standard。
Note
此建構器於 .NET Framework 3.5 或更新版本中引入。
另請參閱
適用於
DynamicMethod(String, Type, Type[], Module)
建立一個動態方法,該方法對模組為全域,指定方法名稱、回傳類型、參數類型及模組。
public:
DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Reflection::Module ^ m);
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, System.Reflection.Module m);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * System.Reflection.Module -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), m As Module)
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
例外狀況
.NET Framework 與 2.1 .NET Core 版本:returnType 是 IsByRef 回傳 true 的類型。
範例
以下程式碼範例建立了一個動態方法,取兩個參數。 範例中會輸出一個簡單的函式主體,將第一個參數印入主控台,並使用第二個參數作為方法的回傳值。 範例完成方法的步驟是首先創建一個委託,然後以不同的參數調用該委託,最後使用Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)方法調用動態方法。
using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;
public class Test
{
// Declare a delegate that will be used to execute the completed
// dynamic method.
private delegate int HelloInvoker(string msg, int ret);
public static void Main()
{
// Create an array that specifies the types of the parameters
// of the dynamic method. This method has a string parameter
// and an int parameter.
Type[] helloArgs = {typeof(string), typeof(int)};
// Create a dynamic method with the name "Hello", a return type
// of int, and two parameters whose types are specified by the
// array helloArgs. Create the method in the module that
// defines the Test class.
DynamicMethod hello = new DynamicMethod("Hello",
typeof(int),
helloArgs,
typeof(Test).Module);
// Create an array that specifies the parameter types of the
// overload of Console.WriteLine to be used in Hello.
Type[] writeStringArgs = {typeof(string)};
// Get the overload of Console.WriteLine that has one
// String parameter.
MethodInfo writeString =
typeof(Console).GetMethod("WriteLine", writeStringArgs);
// Get an ILGenerator and emit a body for the dynamic method.
ILGenerator il = hello.GetILGenerator();
// Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0);
// Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, null);
// The Hello method returns the value of the second argument;
// to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ret);
// Create a delegate that represents the dynamic method. This
// action completes the method, and any further attempts to
// change the method will cause an exception.
HelloInvoker hi =
(HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker));
// Use the delegate to execute the dynamic method. Save and
// print the return value.
int retval = hi("\r\nHello, World!", 42);
Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}",
retval);
// Do it again, with different arguments.
retval = hi("\r\nHi, Mom!", 5280);
Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}",
retval);
// Create an array of arguments to use with the Invoke method.
object[] invokeArgs = {"\r\nHello, World!", 42};
// Invoke the dynamic method using the arguments. This is much
// slower than using the delegate, because you must create an
// array to contain the arguments, and ValueType arguments
// must be boxed.
object objRet = hello.Invoke(null, invokeArgs);
Console.WriteLine("hello.Invoke returned {0}", objRet);
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Public Class Test
' Declare a delegate that will be used to execute the completed
' dynamic method.
Private Delegate Function HelloInvoker(ByVal msg As String, _
ByVal ret As Integer) As Integer
Public Shared Sub Main()
' Create an array that specifies the types of the parameters
' of the dynamic method. This method has a String parameter
' and an Integer parameter.
Dim helloArgs() As Type = {GetType(String), GetType(Integer)}
' Create a dynamic method with the name "Hello", a return type
' of Integer, and two parameters whose types are specified by
' the array helloArgs. Create the method in the module that
' defines the Test class.
Dim hello As New DynamicMethod("Hello", _
GetType(Integer), _
helloArgs, _
GetType(Test).Module)
' Create an array that specifies the parameter types of the
' overload of Console.WriteLine to be used in Hello.
Dim writeStringArgs() As Type = {GetType(String)}
' Get the overload of Console.WriteLine that has one
' String parameter.
Dim writeString As MethodInfo = GetType(Console). _
GetMethod("WriteLine", writeStringArgs)
' Get an ILGenerator and emit a body for the dynamic method.
Dim il As ILGenerator = hello.GetILGenerator()
' Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0)
' Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, Nothing)
' The Hello method returns the value of the second argument;
' to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1)
il.Emit(OpCodes.Ret)
' Create a delegate that represents the dynamic method. This
' action completes the method, and any further attempts to
' change the method will cause an exception.
Dim hi As HelloInvoker = _
hello.CreateDelegate(GetType(HelloInvoker))
' Use the delegate to execute the dynamic method. Save and
' print the return value.
Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42)
Console.WriteLine("Executing delegate hi(""Hello, World!"", 42) returned " _
& retval)
' Do it again, with different arguments.
retval = hi(vbCrLf & "Hi, Mom!", 5280)
Console.WriteLine("Executing delegate hi(""Hi, Mom!"", 5280) returned " _
& retval)
' Create an array of arguments to use with the Invoke method.
Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42}
' Invoke the dynamic method using the arguments. This is much
' slower than using the delegate, because you must create an
' array to contain the arguments, and ValueType arguments
' must be boxed. Note that this overload of Invoke is
' inherited from MethodBase, and simply calls the more
' complete overload of Invoke.
Dim objRet As Object = hello.Invoke(Nothing, invokeArgs)
Console.WriteLine("hello.Invoke returned " & objRet)
End Sub
End Class
' This code example produces the following output:
'
'Hello, World!
'Executing delegate hi("Hello, World!", 42) returned 42
'
'Hi, Mom!
'Executing delegate hi("Hi, Mom!", 5280) returned 5280
'
'Hello, World!
'hello.Invoke returned 42
'
備註
此建構子指定方法屬性 MethodAttributes.Public 與 MethodAttributes.Static,呼叫慣例 CallingConventions.Standard,且不會跳過即時(JIT)可見性檢查。
使用此建構子來建立的動態方法可以訪問模組 internal 中所包含的所有型別的公開 Friend 及 m 成員(在 Visual Basic 中)。
Note
為了向後相容,當以下兩個條件皆成立時,此構造子要求使用SecurityPermission與SecurityPermissionFlag.ControlEvidence旗標:首先,如果m是呼叫模組以外的其他模組,其次,如果要求ReflectionPermission與ReflectionPermissionFlag.MemberAccess旗標失敗。 若需求 SecurityPermission 成功滿足,則允許該操作。
另請參閱
適用於
DynamicMethod(String, Type, Type[], Type)
建立一個動態方法,指定方法名稱、回傳類型、參數類型,以及動態方法邏輯關聯的類型。
public:
DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, Type ^ owner);
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * Type -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), owner As Type)
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
例外狀況
.NET Framework 與 2.1 .NET Core 版本:returnType 是 IsByRef 回傳 true 的類型。
範例
以下程式碼範例會產生 DynamicMethod 一個邏輯上與某個型別相關聯的 。 這個協會讓它能接觸到該類型的私人會員。
程式碼範例定義了一個帶有私有欄位的類別Example,一個從第一個類別衍生出來的類別DerivedFromExample,一個名為UseLikeStatic的代理型別(返回Int32,且參數類型為Example和Int32),以及一個名為UseLikeInstance的代理型別,返回Int32且有一個參數類型為Int32。
範例程式碼接著建立DynamicMethod,改變Example實例的私有欄位並回傳先前的值。
Note
一般而言,改變類別的內部欄位並不是良好的物件導向編碼實務。
範例程式碼先建立一個 的 Example 實例,然後再建立兩個代理。 第一個是型別 UseLikeStatic,其參數與動態方法的相同。 第二種是型別 UseLikeInstance,但缺少第一個參數(型別 Example)。 這個代理是透過 CreateDelegate(Type, Object) 方法超載建立的;該方法超載的第二個參數是 的 Example一個實例,在此例中是剛建立的實例,該實例綁定於新建立的代理。 每當該代理被調用時,動態方法就會作用於綁定的 實例 Example。
Note
這是 .NET Framework 2.0 中放寬的代理綁定規則範例,以及該方法的新 Delegate.CreateDelegate 超載。 如需詳細資訊,請參閱 Delegate 類別。
UseLikeStatic代理人被召喚,若 與代理人有約束UseLikeInstance,則由代理人召喚Example。 接著會召喚代理 UseLikeInstance ,使得兩個代理都在 的 Example同一實例上行動。 每次呼叫後會顯示內部欄位值的變化。 最後,將UseLikeInstance代理繫結到DerivedFromExample實例上,然後重複呼叫該代理。
using System;
using System.Reflection;
using System.Reflection.Emit;
// These classes are for demonstration purposes.
//
public class Example
{
private int id = 0;
public Example(int id)
{
this.id = id;
}
public int ID { get { return id; }}
}
public class DerivedFromExample : Example
{
public DerivedFromExample(int id) : base(id) {}
}
// Two delegates are declared: UseLikeInstance treats the dynamic
// method as if it were an instance method, and UseLikeStatic
// treats the dynamic method in the ordinary fashion.
//
public delegate int UseLikeInstance(int newID);
public delegate int UseLikeStatic(Example ex, int newID);
public class Demo
{
public static void Main()
{
// This dynamic method changes the private id field. It has
// no name; it returns the old id value (return type int);
// it takes two parameters, an instance of Example and
// an int that is the new value of id; and it is declared
// with Example as the owner type, so it can access all
// members, public and private.
//
DynamicMethod changeID = new DynamicMethod(
"",
typeof(int),
new Type[] { typeof(Example), typeof(int) },
typeof(Example)
);
// Get a FieldInfo for the private field 'id'.
FieldInfo fid = typeof(Example).GetField(
"id",
BindingFlags.NonPublic | BindingFlags.Instance
);
ILGenerator ilg = changeID.GetILGenerator();
// Push the current value of the id field onto the
// evaluation stack. It's an instance field, so load the
// instance of Example before accessing the field.
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldfld, fid);
// Load the instance of Example again, load the new value
// of id, and store the new field value.
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldarg_1);
ilg.Emit(OpCodes.Stfld, fid);
// The original value of the id field is now the only
// thing on the stack, so return from the call.
ilg.Emit(OpCodes.Ret);
// Create a delegate that uses changeID in the ordinary
// way, as a static method that takes an instance of
// Example and an int.
//
UseLikeStatic uls =
(UseLikeStatic) changeID.CreateDelegate(
typeof(UseLikeStatic)
);
// Create an instance of Example with an id of 42.
//
Example ex = new Example(42);
// Create a delegate that is bound to the instance of
// of Example. This is possible because the first
// parameter of changeID is of type Example. The
// delegate has all the parameters of changeID except
// the first.
UseLikeInstance uli =
(UseLikeInstance) changeID.CreateDelegate(
typeof(UseLikeInstance),
ex
);
// First, change the value of id by calling changeID as
// a static method, passing in the instance of Example.
//
Console.WriteLine(
"Change the value of id; previous value: {0}",
uls(ex, 1492)
);
// Change the value of id again using the delegate bound
// to the instance of Example.
//
Console.WriteLine(
"Change the value of id; previous value: {0}",
uli(2700)
);
Console.WriteLine("Final value of id: {0}", ex.ID);
// Now repeat the process with a class that derives
// from Example.
//
DerivedFromExample dfex = new DerivedFromExample(71);
uli = (UseLikeInstance) changeID.CreateDelegate(
typeof(UseLikeInstance),
dfex
);
Console.WriteLine(
"Change the value of id; previous value: {0}",
uls(dfex, 73)
);
Console.WriteLine(
"Change the value of id; previous value: {0}",
uli(79)
);
Console.WriteLine("Final value of id: {0}", dfex.ID);
}
}
/* This code example produces the following output:
Change the value of id; previous value: 42
Change the value of id; previous value: 1492
Final value of id: 2700
Change the value of id; previous value: 71
Change the value of id; previous value: 73
Final value of id: 79
*/
Imports System.Reflection
Imports System.Reflection.Emit
' These classes are for demonstration purposes.
'
Public Class Example
Private _id As Integer = 0
Public Sub New(ByVal newId As Integer)
_id = newId
End Sub
Public ReadOnly Property ID() As Integer
Get
Return _id
End Get
End Property
End Class
Public Class DerivedFromExample
Inherits Example
Public Sub New(ByVal newId As Integer)
MyBase.New(newId)
End Sub
End Class
' Two delegates are declared: UseLikeInstance treats the dynamic
' method as if it were an instance method, and UseLikeStatic
' treats the dynamic method in the ordinary fashion.
'
Public Delegate Function UseLikeInstance(ByVal newID As Integer) _
As Integer
Public Delegate Function UseLikeStatic(ByVal ex As Example, _
ByVal newID As Integer) As Integer
Public Class Demo
Public Shared Sub Main()
' This dynamic method changes the private _id field. It
' has no name; it returns the old _id value (return type
' Integer); it takes two parameters, an instance of Example
' and an Integer that is the new value of _id; and it is
' declared with Example as the owner type, so it can
' access all members, public and private.
'
Dim changeID As New DynamicMethod( _
"", _
GetType(Integer), _
New Type() {GetType(Example), GetType(Integer)}, _
GetType(Example) _
)
' Get a FieldInfo for the private field '_id'.
Dim fid As FieldInfo = GetType(Example).GetField( _
"_id", _
BindingFlags.NonPublic Or BindingFlags.Instance _
)
Dim ilg As ILGenerator = changeID.GetILGenerator()
' Push the current value of the id field onto the
' evaluation stack. It's an instance field, so load the
' instance of Example before accessing the field.
ilg.Emit(OpCodes.Ldarg_0)
ilg.Emit(OpCodes.Ldfld, fid)
' Load the instance of Example again, load the new value
' of id, and store the new field value.
ilg.Emit(OpCodes.Ldarg_0)
ilg.Emit(OpCodes.Ldarg_1)
ilg.Emit(OpCodes.Stfld, fid)
' The original value of the id field is now the only
' thing on the stack, so return from the call.
ilg.Emit(OpCodes.Ret)
' Create a delegate that uses changeID in the ordinary
' way, as a static method that takes an instance of
' Example and an Integer.
'
Dim uls As UseLikeStatic = CType( _
changeID.CreateDelegate(GetType(UseLikeStatic)), _
UseLikeStatic _
)
' Create an instance of Example with an id of 42.
'
Dim ex As New Example(42)
' Create a delegate that is bound to the instance of
' of Example. This is possible because the first
' parameter of changeID is of type Example. The
' delegate has all the parameters of changeID except
' the first.
Dim uli As UseLikeInstance = CType( _
changeID.CreateDelegate( _
GetType(UseLikeInstance), _
ex), _
UseLikeInstance _
)
' First, change the value of _id by calling changeID as
' a static method, passing in the instance of Example.
'
Console.WriteLine( _
"Change the value of _id; previous value: {0}", _
uls(ex, 1492) _
)
' Change the value of _id again using the delegate
' bound to the instance of Example.
'
Console.WriteLine( _
"Change the value of _id; previous value: {0}", _
uli(2700) _
)
Console.WriteLine("Final value of _id: {0}", ex.ID)
' Now repeat the process with a class that derives
' from Example.
'
Dim dfex As New DerivedFromExample(71)
uli = CType( _
changeID.CreateDelegate( _
GetType(UseLikeInstance), _
dfex), _
UseLikeInstance _
)
Console.WriteLine( _
"Change the value of _id; previous value: {0}", _
uls(dfex, 73) _
)
Console.WriteLine( _
"Change the value of _id; previous value: {0}", _
uli(79) _
)
Console.WriteLine("Final value of _id: {0}", dfex.ID)
End Sub
End Class
' This code example produces the following output:
'
'Change the value of _id; previous value: 42
'Change the value of _id; previous value: 1492
'Final value of _id: 2700
'Change the value of _id; previous value: 71
'Change the value of _id; previous value: 73
'Final value of _id: 79'
備註
使用此建構子所建立的動態方法可存取 owner 型別的所有成員,以及包含 internal 的模組中所有其他型別的公用和 Friend 成員(在 Visual Basic 中為 owner)。
此建構子指定方法屬性 MethodAttributes.Public 與 MethodAttributes.Static,呼叫慣例 CallingConventions.Standard,且不會跳過即時(JIT)可見性檢查。
Note
為了向後相容,當以下條件皆成立時,該構造子要求SecurityPermission與SecurityPermissionFlag.ControlEvidence旗標:owner位於與呼叫模組不同的模組內,且對ReflectionPermission與ReflectionPermissionFlag.MemberAccess旗標的需求失敗。 若需求 SecurityPermission 成功滿足,則允許該操作。
另請參閱
適用於
DynamicMethod(String, Type, Type[], Module, Boolean)
建立一個全域於模組的動態方法,指定方法名稱、回傳類型、參數類型、模組,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。
public:
DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Reflection::Module ^ m, bool skipVisibility);
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, System.Reflection.Module m, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * System.Reflection.Module * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), m As Module, skipVisibility As Boolean)
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
- skipVisibility
- Boolean
true 跳過動態方法中 MSIL 存取的類型與成員的 JIT 可見性檢查。
例外狀況
.NET Framework 與 2.1 .NET Core 版本:returnType 是 IsByRef 回傳 true 的類型。
備註
此建構子指定方法屬性 MethodAttributes.Public 與 MethodAttributes.Static,並呼叫慣例 CallingConventions.Standard。
使用此建構子所建立的動態方法可存取包含模組 m 中所有型別的公用及 internal(Visual BasicFriend)成員。 跳過 JIT 編譯器的可見性檢查,讓動態方法也能存取所有其他類型的私密及受保護成員。 這在撰寫序列化物件的程式碼時非常有用。
Note
為了向後相容,當以下兩個條件皆成立時,此構造子要求使用SecurityPermission與SecurityPermissionFlag.ControlEvidence旗標:首先,如果m是呼叫模組以外的其他模組,其次,如果要求ReflectionPermission與ReflectionPermissionFlag.MemberAccess旗標失敗。 若需求 SecurityPermission 成功滿足,則允許該操作。
另請參閱
適用於
DynamicMethod(String, Type, Type[], Type, Boolean)
建立一個動態方法,指定方法名稱、回傳類型、參數類型、動態方法邏輯關聯的類型,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的即時(JIT)可見性檢查。
public:
DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, Type ^ owner, bool skipVisibility);
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * Type * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), owner As Type, skipVisibility As Boolean)
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
- skipVisibility
- Boolean
true跳過動態方法中MSIL存取的類型與成員的JIT可見性檢查;否則,。 false
例外狀況
.NET Framework 與 2.1 .NET Core 版本:returnType 是 IsByRef 回傳 true 的類型。
備註
使用此建構子所建立的動態方法可存取 owner 型別的所有成員,以及包含 internal 的模組中所有其他型別的公用和 Friend 成員(在 Visual Basic 中為 owner)。 跳過 JIT 編譯器的可見性檢查,讓動態方法也能存取所有其他類型的私密及受保護成員。 這在撰寫序列化物件的程式碼時非常有用。
此建構子指定方法屬性 MethodAttributes.Public 與 MethodAttributes.Static,並呼叫慣例 CallingConventions.Standard。
Note
為了向後相容,當以下條件皆成立時,該構造子要求SecurityPermission與SecurityPermissionFlag.ControlEvidence旗標:owner位於與呼叫模組不同的模組內,且對ReflectionPermission與ReflectionPermissionFlag.MemberAccess旗標的需求失敗。 若需求 SecurityPermission 成功滿足,則允許該操作。
另請參閱
適用於
DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Module, Boolean)
建立一個全域於模組的動態方法,指定方法名稱、屬性、呼叫慣例、回傳型別、參數類型、模組,以及是否應跳過動態方法的 Microsoft 中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。
public:
DynamicMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Reflection::Module ^ m, bool skipVisibility);
public DynamicMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Reflection.Module m, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Reflection.Module * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), m As Module, skipVisibility As Boolean)
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
- attributes
- MethodAttributes
一個位元組合 MethodAttributes 的值,用來指定動態方法的屬性。 唯一允許的組合是 Public 和 Static。
- callingConvention
- CallingConventions
動態方法的呼叫慣例。 必須是 Standard。
- skipVisibility
- Boolean
true跳過動態方法中MSIL存取的類型與成員的JIT可見性檢查;否則,。 false
例外狀況
attributes是除 和 Static之外的旗標Public組合。
-或-
callingConvention 不是 Standard。
-或-
returnType 是一個類型,使得 IsByRef 返回 true。
備註
使用此建構器所建立的動態方法可存取模組 m 中所有公開與內部類型的公用及內部類型中的 internal(Visual BasicFriend)。
跳過 JIT 編譯器的可見性檢查,讓動態方法能存取模組中所有其他類型的私密及受保護成員,以及所有其他組合中。 這在撰寫序列化物件的程式碼時非常有用。
Note
為了向後相容,當以下兩個條件皆成立時,此構造子要求使用SecurityPermission與SecurityPermissionFlag.ControlEvidence旗標:首先,如果m是呼叫模組以外的其他模組,其次,如果要求ReflectionPermission與ReflectionPermissionFlag.MemberAccess旗標失敗。 若需求 SecurityPermission 成功滿足,則允許該操作。
另請參閱
適用於
DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type, Boolean)
建立動態方法,指定方法名稱、屬性、呼叫慣例、回傳型別、參數型別、動態方法邏輯關聯的型別,以及是否應跳過動態方法Microsoft中介語言(MSIL)存取的型別與成員的即時(JIT)可見性檢查。
public:
DynamicMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, Type ^ owner, bool skipVisibility);
public DynamicMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), owner As Type, skipVisibility As Boolean)
參數
- name
- String
動態方法的名稱。 這可以是零長度的字串,但不可能是 null。
- attributes
- MethodAttributes
一個位元組合 MethodAttributes 的值,用來指定動態方法的屬性。 唯一允許的組合是 Public 和 Static。
- callingConvention
- CallingConventions
動態方法的呼叫慣例。 必須是 Standard。
- skipVisibility
- Boolean
true跳過動態方法中MSIL存取的類型與成員的JIT可見性檢查;否則,。 false
例外狀況
attributes是除 和 Static之外的旗標Public組合。
-或-
callingConvention 不是 Standard。
-或-
returnType 是一個類型,使得 IsByRef 返回 true。
備註
動態方法全域於包含型別 owner的模組。 它能存取所有該類型的 owner成員。
使用此建構子所建立的動態方法可存取所有類型為 owner 的成員,以及包含 owner 模組中所有類型中的公用及 internal(Visual BasicFriend)成員。 跳過 JIT 編譯器的可見性檢查,讓動態方法也能存取所有其他類型的私密及受保護成員。 這在撰寫序列化物件的程式碼時非常有用。
Note
為了向後相容,當以下條件皆成立時,該構造子要求SecurityPermission與SecurityPermissionFlag.ControlEvidence旗標:owner位於與呼叫模組不同的模組內,且對ReflectionPermission與ReflectionPermissionFlag.MemberAccess旗標的需求失敗。 若需求 SecurityPermission 成功滿足,則允許該操作。