DynamicObject.TrySetIndex(SetIndexBinder, Object[], Object) 方法

定义

为按索引设置值的操作提供实现。 从 DynamicObject 类派生的类可以重写此方法,以便为按指定索引访问对象的操作指定动态行为。

public:
 virtual bool TrySetIndex(System::Dynamic::SetIndexBinder ^ binder, cli::array <System::Object ^> ^ indexes, System::Object ^ value);
public virtual bool TrySetIndex (System.Dynamic.SetIndexBinder binder, object[] indexes, object value);
public virtual bool TrySetIndex (System.Dynamic.SetIndexBinder binder, object[] indexes, object? value);
abstract member TrySetIndex : System.Dynamic.SetIndexBinder * obj[] * obj -> bool
override this.TrySetIndex : System.Dynamic.SetIndexBinder * obj[] * obj -> bool
Public Overridable Function TrySetIndex (binder As SetIndexBinder, indexes As Object(), value As Object) As Boolean

参数

binder
SetIndexBinder

提供有关该操作的信息。

indexes
Object[]

该操作中使用的索引。 例如,对于 C# 中的 sampleObject[3] = 10操作(Visual Basic 中为 sampleObject(3) = 10,其中 sampleObject 派生自 DynamicObject 类),indexes[0] 等于 3。

value
Object

要为具有指定索引的对象设置的值。 例如,对于 C# 中的 sampleObject[3] = 10 操作(Visual Basic 中为 sampleObject(3) = 10),其中 sampleObject 派生自 DynamicObject 类,value 等于 10。

返回

Boolean

如果操作成功,则为 true;否则为 false。 如果此方法返回 false,则该语言的运行时联编程序将决定行为。 (大多数情况下,将引发语言特定的运行时异常。)

示例

假设要创建一个对象,在该对象中可以通过名称(例如Property0Property1等等)或索引访问属性,以便例如,sampleObject.Property0在 C# 或 sampleObject(0) Visual Basic 中等效sampleObject[0]

下面的代码示例演示 SampleDynamicObject 派生自该类的 DynamicObject 类。 该SampleDynamicObject类包含Visual Basic) 中类型 (Dictionary(Of String, Object)的对象Dictionary<string, object>,用于存储键值对。 SampleDynamicObject 重写通过索引启用访问的 TrySetIndexTryGetIndex 方法。 它将重写TrySetMemberTryGetMember和方法以按属性名称启用访问。

// 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 类可以重写此方法,以指定如何对动态对象执行通过索引访问对象的操作。 如果未重写该方法,则语言的运行时绑定器将确定行为。 (大多数情况下,将引发语言特定的运行时异常。)

如果重写此方法,则当操作(如 sampleObject[3] = 10 C# 或sampleObject(3) = 10Visual BasicsampleObject)派生自DynamicObject类时,会自动调用此方法。

适用于