DynamicObject.TryGetIndex(GetIndexBinder, Object[], Object) 메서드

정의

인덱스별로 값을 가져오는 연산에 대한 구현을 제공합니다. DynamicObject 클래스에서 파생된 클래스로 이 메서드를 재정의하여 인덱싱 연산의 동적 동작을 지정할 수 있습니다.

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

연산에 대한 정보를 제공합니다.

indexes
Object[]

연산에 사용되는 인덱스입니다. 예를 들어 C#의 작업(sampleObject(3)Visual Basic의 경우)의 경우 sampleObject[3]sampleObject 클래스에서 DynamicObject 파생되며 는 indexes[0] 3과 같습니다.

result
Object

인덱스 연산의 결과입니다.

반환

작업에 성공하면 true이고, 그렇지 않으면 false입니다. 이 메서드가 false를 반환하는 경우 언어의 런타임 바인더에 따라 동작이 결정됩니다. 대부분의 경우 런타임 예외가 throw됩니다.

예제

예를 들어 sampleObject.Property0 C# 또는 sampleObject(0) Visual Basic과 동일하도록 , Property1, 등의 이름 Property0또는 인덱스로 속성에 액세스할 수 있는 개체를 sampleObject[0] 만들려고 한다고 가정합니다.

다음 코드 예제에서는 SampleDynamicObject 클래스에서 파생 되는 클래스를 보여 줍니다 DynamicObject . 클래스에는 SampleDynamicObject 키-값 쌍을 Dictionary<string, object> 저장할 형식(Dictionary(Of String, Object) Visual Basic의 경우)의 개체가 포함되어 있습니다. SampleDynamicObject 인덱스별로 TrySetIndex 액세스를 사용하도록 설정하려면 및 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 하여 동적 개체에 대해 인덱스로 값을 가져오는 방법을 지정할 수 있습니다. 메서드를 재정의하지 않으면 언어의 런타임 바인더가 동작을 결정합니다. 대부분의 경우 런타임 예외가 throw됩니다.

이 메서드가 재정의되면 C# 또는 sampleObject(3) Visual Basic에서 와 같은 sampleObject[3] 작업이 있을 때 자동으로 호출됩니다. 여기서 sampleObject 는 클래스에서 DynamicObject 파생됩니다.

적용 대상