DynamicObject.TryGetIndex(GetIndexBinder, Object[], Object) 方法
定义
为按索引获取值的操作提供实现。Provides the implementation for operations that get a value by index. 从 DynamicObject 类派生的类可以重写此方法,以便为索引操作指定动态行为。Classes derived from the DynamicObject class can override this method to specify dynamic behavior for indexing operations.
public:
virtual bool TryGetIndex(System::Dynamic::GetIndexBinder ^ binder, cli::array <System::Object ^> ^ indexes, [Runtime::InteropServices::Out] System::Object ^ % result);
public virtual bool TryGetIndex (System.Dynamic.GetIndexBinder binder, object[] indexes, out object result);
public virtual bool TryGetIndex (System.Dynamic.GetIndexBinder binder, object[] indexes, out object? result);
abstract member TryGetIndex : System.Dynamic.GetIndexBinder * obj[] * obj -> bool
override this.TryGetIndex : System.Dynamic.GetIndexBinder * obj[] * obj -> bool
Public Overridable Function TryGetIndex (binder As GetIndexBinder, indexes As Object(), ByRef result As Object) As Boolean
参数
- binder
- GetIndexBinder
提供有关该操作的信息。Provides information about the operation.
- indexes
- Object[]
该操作中使用的索引。The indexes that are used in the operation. 例如,对于 C# 中的 sampleObject[3] 操作(在 Visual Basic 中为 sampleObject(3),其中 sampleObject 派生自 DynamicObject 类),indexes[0] 等于 3。For example, for the sampleObject[3] operation in C# (sampleObject(3) in Visual Basic), where sampleObject is derived from the DynamicObject class, indexes[0] is equal to 3.
- result
- Object
索引操作的结果。The result of the index operation.
返回
如果操作成功,则为 true;否则为 false。true if the operation is successful; otherwise, false. 如果此方法返回 false,则该语言的运行时联编程序将决定行为。If this method returns false, the run-time binder of the language determines the behavior. (大多数情况下,将引发运行时异常。)(In most cases, a run-time exception is thrown.)
示例
假设你要创建一个对象,该对象可以通过名称(如、等) Property0 Property1 或索引访问属性,以便例如, sampleObject.Property0 sampleObject[0] 在 c # 中或 sampleObject(0) 在 Visual Basic 中等效。Assume that you want to create an object in which properties can be accessed either by names such as Property0, Property1, and so on, or by index, so that, for example, sampleObject.Property0 is equivalent to sampleObject[0] in C# or sampleObject(0) in Visual Basic.
下面的代码示例演示 SampleDynamicObject 类,该类派生自 DynamicObject 类。The following code example demonstrates the SampleDynamicObject class, which is derived from the DynamicObject class. SampleDynamicObject类包含 Dictionary<string, object> Visual Basic) 中 (类型的对象, Dictionary(Of String, Object) 以存储键值对。The SampleDynamicObject class contains an object of the Dictionary<string, object> type (Dictionary(Of String, Object) in Visual Basic) to store the key-value pairs. SampleDynamicObject 重写 TrySetIndex 和 TryGetIndex 方法,以启用按索引进行访问。SampleDynamicObject overrides the TrySetIndex and TryGetIndex methods to enable access by index. 它重写 TrySetMember 和 TryGetMember 方法,以通过属性名称启用访问。It overrides the TrySetMember and TryGetMember methods to enable access by property name.
// The class derived from DynamicObject.
public class SampleDynamicObject : DynamicObject
{
// The inner dictionary to store field names and values.
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// Get the property value.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
return dictionary.TryGetValue(binder.Name, out result);
}
// Set the property value.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
dictionary[binder.Name] = value;
return true;
}
// Set the property value by index.
public override bool TrySetIndex(
SetIndexBinder binder, object[] indexes, object value)
{
int index = (int)indexes[0];
// If a corresponding property already exists, set the value.
if (dictionary.ContainsKey("Property" + index))
dictionary["Property" + index] = value;
else
// If a corresponding property does not exist, create it.
dictionary.Add("Property" + index, value);
return true;
}
// Get the property value by index.
public override bool TryGetIndex(
GetIndexBinder binder, object[] indexes, out object result)
{
int index = (int)indexes[0];
return dictionary.TryGetValue("Property" + index, out result);
}
}
class Program
{
static void Test(string[] args)
{
// Creating a dynamic object.
dynamic sampleObject = new SampleDynamicObject();
// Creating Property0.
// The TrySetMember method is called.
sampleObject.Property0 = "Zero";
// Getting the value by index.
// The TryGetIndex method is called.
Console.WriteLine(sampleObject[0]);
// Setting the property value by index.
// The TrySetIndex method is called.
// (This method also creates Property1.)
sampleObject[1] = 1;
// Getting the Property1 value.
// The TryGetMember method is called.
Console.WriteLine(sampleObject.Property1);
// The following statement produces a run-time exception
// because there is no corresponding property.
//Console.WriteLine(sampleObject[2]);
}
}
// This code example produces the following output:
// Zero
// 1
' The class derived from DynamicObject.
Public Class SampleDynamicObject
Inherits DynamicObject
' The inner dictionary to store field names and values.
Dim dictionary As New Dictionary(Of String, Object)
' Get the 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
' Set the 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
' Set the property value by index.
Public Overrides Function TrySetIndex(
ByVal binder As System.Dynamic.SetIndexBinder,
ByVal indexes() As Object, ByVal value As Object) As Boolean
Dim index As Integer = CInt(indexes(0))
' If a corresponding property already exists, set the value.
If (dictionary.ContainsKey("Property" & index)) Then
dictionary("Property" & index) = value
Else
' If a property does not exist, create it.
dictionary.Add("Property" & index, value)
End If
Return True
End Function
' Get the property value by index.
Public Overrides Function TryGetIndex(
ByVal binder As System.Dynamic.GetIndexBinder,
ByVal indexes() As Object, ByRef result As Object) As Boolean
Dim index = CInt(indexes(0))
Return dictionary.TryGetValue("Property" & index, result)
End Function
End Class
Sub Test()
' Creating a dynamic object.
Dim sampleObject As Object = New SampleDynamicObject()
' Creating Property0.
' The TrySetMember method is called.
sampleObject.Property0 = "Zero"
' Getting the value by index.
' The TryGetIndex method is called.
Console.WriteLine(sampleObject(0))
' Setting the property value by index.
' The TrySetIndex method is called.
' (This method also creates Property1.)
sampleObject(1) = 1
' Getting the Property1 value.
' The TryGetMember method is called.
Console.WriteLine(sampleObject.Property1)
' The following statement produces a run-time exception
' because there is no corresponding property.
' Console.WriteLine(sampleObject(2))
End Sub
' This code example produces the following output:
' Zero
' 1
注解
从类派生的类 DynamicObject 可以重写此方法,以指定应该如何为动态对象执行按索引获取值。Classes derived from the DynamicObject class can override this method to specify how getting a value by index should be performed for a dynamic object. 如果未重写此方法,则该语言的运行时联编程序将确定行为。When the method is not overridden, the run-time binder of the language determines the behavior. (大多数情况下,将引发运行时异常。)(In most cases, a run-time exception is thrown.)
如果重写此方法,则当你的操作如 sampleObject[3] c # 或 sampleObject(3) Visual Basic (其中 sampleObject 派生自类)时,将自动调用此方法 DynamicObject 。If this method is overridden, it is automatically invoked when you have an operation like sampleObject[3] in C# or sampleObject(3) in Visual Basic, where sampleObject is derived from the DynamicObject class.