共用方式為


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[]

用於作業的索引。 例如,針對 sampleObject[3] = 10 Visual Basic) 中 C# (sampleObject(3) = 10 中的作業,其中 sampleObject 衍生自 DynamicObject 類別, indexes[0] 等於 3。

value
Object

要設定給具有所指定索引之物件的值。 例如,對於 sampleObject[3] = 10 Visual Basic) 中 C# (sampleObject(3) = 10 中的作業,其中 sampleObject 衍生自 DynamicObject 類別, value 等於 10。

傳回

如果作業成功,則為 true,否則為 false。 如果這個方法傳回 false,語言的執行階段繫結器會決定行為。 在大多數情況下,會擲回語言特有執行階段例外狀況。

範例

假設您想要建立一個物件,其中屬性可以透過 、 等名稱Property0Property1或索引來存取,例如,例如sampleObject.Property0,在 C# 或 sampleObject(0) Visual Basic 中是相等sampleObject[0]的。

下列程式代碼範例示範 SampleDynamicObject 衍生自 類別的 DynamicObject 類別。 類別 SampleDynamicObject 包含 Visual Basic 中類型 (的物件 Dictionary<string, object>) Dictionary(Of String, Object) 來儲存機碼/值組。 SampleDynamicObjectTrySetIndex 覆寫和 TryGetIndex 方法來啟用索引的存取。 它會覆寫 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) = 10 Visual Basic 中的作業衍生自 DynamicObject 類別時,sampleObject會自動叫用此方法。

適用於