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[]
调用操作期间传递给对象成员的参数。 例如,对于 从 类派生DynamicObject的 语句sampleObject.SampleMethod(100)
sampleObject
,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
包含 Visual Basic) 中 (Dictionary(Of String, Object)
类型的 对象Dictionary<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
参数都按值传递。
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |