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语句DynamicObjectargs[0],等于 100。
- result
- Object
成员调用的结果。
返回
如果操作成功,则为 true;否则为 false。 如果此方法返回 false,则语言的运行时绑定器将确定行为。 (在大多数情况下,将引发特定于语言的运行时异常。
示例
假设你想要提供用于访问字典中的值的替代语法,以便无需在 Visual Basic 中编写 sampleDictionary["Text"] = "Sample text" (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 参数都按值传递。