DynamicObject.TryInvokeMember(InvokeMemberBinder, Object[], Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供呼叫成員操作的實作。 從該 DynamicObject 類別衍生的類別可以覆寫此方法,以指定操作的動態行為,例如呼叫方法。
public:
virtual bool TryInvokeMember(System::Dynamic::InvokeMemberBinder ^ binder, cli::array <System::Object ^> ^ args, [Runtime::InteropServices::Out] System::Object ^ % result);
public virtual bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result);
public virtual bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object?[]? args, out object? result);
abstract member TryInvokeMember : System.Dynamic.InvokeMemberBinder * obj[] * obj -> bool
override this.TryInvokeMember : System.Dynamic.InvokeMemberBinder * obj[] * obj -> bool
Public Overridable Function TryInvokeMember (binder As InvokeMemberBinder, args As Object(), ByRef result As Object) As Boolean
參數
- binder
- InvokeMemberBinder
提供動態操作的資訊。 屬性 binder.Name 提供執行動態操作的成員名稱。 例如,對於陳述式 sampleObject.SampleMethod(100),其中 sampleObject 是從類別 DynamicObject 衍生出的類別實例,回 binder.Name 傳「SampleMethod」。 該 binder.IgnoreCase 屬性指定成員名稱是否區分大小寫。
- args
- Object[]
在呼叫操作中傳遞給物件成員的參數。 例如,對於陳述 sampleObject.SampleMethod(100),其中 sampleObject 是從類別 DynamicObject 推導出來的, args[0] 等於 100。
- result
- Object
成員召喚的結果。
傳回
如果作業成功,則為 true,否則為 false。 若此方法回傳 false,該語言的執行時綁定器決定行為。 (大多數情況下會拋出語言特定的執行時例外。)
範例
假設你想提供替代語法來存取字典中的值,這樣你就不用寫 sampleDictionary["Text"] = "Sample text"(Visual Basic 中的 sampleDictionary("Text") = "Sample text"),而是寫成 sampleDictionary.Text = "Sample text"。 此外,你也希望能調用這本字典上所有標準的字典方法。
以下程式碼範例展示了 DynamicDictionary 由該 DynamicObject 類別衍生而來的類別。
DynamicDictionary 類別包含一個 Dictionary<string, object> 型別的物件(Visual Basic 中的 Dictionary(Of String, Object))用來儲存鍵值對。 它會覆 TryInvokeMember 寫支援 Dictionary<TKey,TValue> 類別的方法,並覆寫 TrySetMember 與 TryGetMember 方法以支援新的語法。 它也提供一種 Print 方法,可以列印所有字典的鍵與值。
// Add using System.Reflection;
// to the beginning of the file.
// The class derived from DynamicObject.
public class DynamicDictionary : DynamicObject
{
// The inner dictionary.
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// Getting a property.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
return dictionary.TryGetValue(binder.Name, out result);
}
// Setting a property.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
dictionary[binder.Name] = value;
return true;
}
// Calling a method.
public override bool TryInvokeMember(
InvokeMemberBinder binder, object[] args, out object result)
{
Type dictType = typeof(Dictionary<string, object>);
try
{
result = dictType.InvokeMember(
binder.Name,
BindingFlags.InvokeMethod,
null, dictionary, args);
return true;
}
catch
{
result = null;
return false;
}
}
// This methods prints out dictionary elements.
public void Print()
{
foreach (var pair in dictionary)
Console.WriteLine(pair.Key + " " + pair.Value);
if (dictionary.Count == 0)
Console.WriteLine("No elements in the dictionary.");
}
}
class Program
{
static void Main(string[] args)
{
// Creating a dynamic dictionary.
dynamic person = new DynamicDictionary();
// Adding new dynamic properties.
// The TrySetMember method is called.
person.FirstName = "Ellen";
person.LastName = "Adams";
// Calling a method defined in the DynmaicDictionary class.
// The Print method is called.
person.Print();
Console.WriteLine(
"Removing all the elements from the dictionary.");
// Calling a method that is not defined in the DynamicDictionary class.
// The TryInvokeMember method is called.
person.Clear();
// Calling the Print method again.
person.Print();
// The following statement throws an exception at run time.
// There is no Sample method
// in the dictionary or in the DynamicDictionary class.
// person.Sample();
}
}
// This example has the following output:
// FirstName Ellen
// LastName Adams
// Removing all the elements from the dictionary.
// No elements in the dictionary.
' Add Imports System.Reflection
' to the beginning of the file.
' The class derived from DynamicObject.
Public Class DynamicDictionary
Inherits DynamicObject
' The inner dictionary.
Dim dictionary As New Dictionary(Of String, Object)
' Getting a property value.
Public Overrides Function TryGetMember(
ByVal binder As System.Dynamic.GetMemberBinder,
ByRef result As Object) As Boolean
Return dictionary.TryGetValue(binder.Name, result)
End Function
' Setting a property value.
Public Overrides Function TrySetMember(
ByVal binder As System.Dynamic.SetMemberBinder,
ByVal value As Object) As Boolean
dictionary(binder.Name) = value
Return True
End Function
' Calling a method.
Public Overrides Function TryInvokeMember(
ByVal binder As System.Dynamic.InvokeMemberBinder,
ByVal args() As Object, ByRef result As Object) As Boolean
Dim dictType As Type = GetType(Dictionary(Of String, Object))
Try
result = dictType.InvokeMember(
binder.Name,
BindingFlags.InvokeMethod,
Nothing, dictionary, args)
Return True
Catch ex As Exception
result = Nothing
Return False
End Try
End Function
' This method prints out dictionary elements.
Public Sub Print()
For Each pair In dictionary
Console.WriteLine(pair.Key & " " & pair.Value)
Next
If (dictionary.Count = 0) Then
Console.WriteLine("No elements in the dictionary.")
End If
End Sub
End Class
Sub Test()
' Creating a dynamic dictionary.
Dim person As Object = New DynamicDictionary()
' Adding new dynamic properties.
' The TrySetMember method is called.
person.FirstName = "Ellen"
person.LastName = "Adams"
' Calling a method defined in the DynmaicDictionary class.
' The Print method is called.
person.Print()
Console.WriteLine(
"Removing all the elements from the dictionary.")
' Calling a method that is not defined in the DynamicDictionary class.
' The TryInvokeMember method is called.
person.Clear()
' Calling the Print method again.
person.Print()
' The following statement throws an exception at run time.
' There is no Sample method
' in the dictionary or in the DynamicDictionary class.
' person.Sample()
End Sub
' This example has the following output:
' FirstName Ellen
' LastName Adams
' Removing all the elements from the dictionary.
' No elements in the dictionary.
備註
從該 DynamicObject 類別衍生的類別可以覆寫此方法,指定如何對動態物件執行呼叫物件成員的操作。 當方法未被覆寫時,語言的執行時綁定器決定行為。 (大多數情況下會拋出語言特定的執行時例外。)
若此方法被覆寫,執行如 sampleObject.SampleMethod(100),其中 sampleObject 是從 DynamicObject 類別衍生出的操作,該方法會自動被呼叫。
你也可以在從該 DynamicObject 類別衍生出來的類別中加入自己的方法。 例如,如果你覆寫該方法, TryInvokeMember 動態派遣系統會先嘗試判斷該指定方法是否存在於該類別中。 如果找不到該方法,則使用該 TryInvokeMember 實作。
此方法不支援 ref 參數 out 。 陣列中 args 的所有參數皆以值傳遞。