Bagikan melalui


DynamicObject.TryGetMember(GetMemberBinder, Object) Metode

Definisi

Menyediakan implementasi untuk operasi yang mendapatkan nilai anggota. Kelas yang berasal dari DynamicObject kelas dapat mengambil alih metode ini untuk menentukan perilaku dinamis untuk operasi seperti mendapatkan nilai untuk properti.

public:
 virtual bool TryGetMember(System::Dynamic::GetMemberBinder ^ binder, [Runtime::InteropServices::Out] System::Object ^ % result);
public virtual bool TryGetMember (System.Dynamic.GetMemberBinder binder, out object result);
public virtual bool TryGetMember (System.Dynamic.GetMemberBinder binder, out object? result);
abstract member TryGetMember : System.Dynamic.GetMemberBinder * obj -> bool
override this.TryGetMember : System.Dynamic.GetMemberBinder * obj -> bool
Public Overridable Function TryGetMember (binder As GetMemberBinder, ByRef result As Object) As Boolean

Parameter

binder
GetMemberBinder

Menyediakan informasi tentang objek yang disebut operasi dinamis. Properti binder.Name memberikan nama anggota tempat operasi dinamis dilakukan. Misalnya, untuk Console.WriteLine(sampleObject.SampleProperty) pernyataan , di mana sampleObject adalah instans kelas yang berasal dari DynamicObject kelas , binder.Name mengembalikan "SampleProperty". Properti binder.IgnoreCase menentukan apakah nama anggota peka huruf besar/kecil.

result
Object

Hasil operasi get. Misalnya, jika metode dipanggil untuk properti , Anda dapat menetapkan nilai properti ke result.

Mengembalikan

true jika operasi berhasil; jika tidak, false. Jika metode ini mengembalikan false, pengikat run-time bahasa menentukan perilaku. (Dalam kebanyakan kasus, pengecualian run-time dilemparkan.)

Contoh

Asumsikan bahwa Anda ingin memberikan sintaks alternatif untuk mengakses nilai dalam kamus, sehingga alih-alih menulis sampleDictionary["Text"] = "Sample text" (sampleDictionary("Text") = "Sample text" di Visual Basic), Anda dapat menulis sampleDictionary.Text = "Sample text". Selain itu, sintaks ini harus tidak peka huruf besar/kecil, sehingga sampleDictionary.Text setara sampleDictionary.textdengan .

Contoh kode berikut menunjukkan DynamicDictionary kelas , yang berasal dari DynamicObject kelas . Kelas DynamicDictionary berisi objek jenis Dictionary<string, object> (Dictionary(Of String, Object) dalam Visual Basic) untuk menyimpan pasangan kunci-nilai, dan mengambil TrySetMember alih metode dan TryGetMember untuk mendukung sintaks baru. Ini juga menyediakan Count properti , yang menunjukkan berapa banyak properti dinamis yang dikandung kamus.

// The class derived from DynamicObject.
public class DynamicDictionary : DynamicObject
{
    // The inner dictionary.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

    // This property returns the number of elements
    // in the inner dictionary.
    public int Count
    {
        get
        {
            return dictionary.Count;
        }
    }

    // If you try to get a value of a property
    // not defined in the class, this method is called.
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        // Converting the property name to lowercase
        // so that property names become case-insensitive.
        string name = binder.Name.ToLower();

        // If the property name is found in a dictionary,
        // set the result parameter to the property value and return true.
        // Otherwise, return false.
        return dictionary.TryGetValue(name, out result);
    }

    // If you try to set a value of a property that is
    // not defined in the class, this method is called.
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        // Converting the property name to lowercase
        // so that property names become case-insensitive.
        dictionary[binder.Name.ToLower()] = value;

        // You can always add a value to a dictionary,
        // so this method always returns true.
        return true;
    }
}

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";

        // Getting values of the dynamic properties.
        // The TryGetMember method is called.
        // Note that property names are case-insensitive.
        Console.WriteLine(person.firstname + " " + person.lastname);

        // Getting the value of the Count property.
        // The TryGetMember is not called,
        // because the property is defined in the class.
        Console.WriteLine(
            "Number of dynamic properties:" + person.Count);

        // The following statement throws an exception at run time.
        // There is no "address" property,
        // so the TryGetMember method returns false and this causes a
        // RuntimeBinderException.
        // Console.WriteLine(person.address);
    }
}

// This example has the following output:
// Ellen Adams
// Number of dynamic properties: 2
' The class derived from DynamicObject.
Public Class DynamicDictionary
    Inherits DynamicObject

    ' The inner dictionary.
    Dim dictionary As New Dictionary(Of String, Object)

    ' This property returns the number of elements
    ' in the inner dictionary.
    ReadOnly Property Count As Integer
        Get
            Return dictionary.Count
        End Get
    End Property


    ' If you try to get a value of a property that is
    ' not defined in the class, this method is called.

    Public Overrides Function TryGetMember(
        ByVal binder As System.Dynamic.GetMemberBinder,
        ByRef result As Object) As Boolean

        ' Converting the property name to lowercase
        ' so that property names become case-insensitive.
        Dim name As String = binder.Name.ToLower()

        ' If the property name is found in a dictionary,
        ' set the result parameter to the property value and return true.
        ' Otherwise, return false.
        Return dictionary.TryGetValue(name, result)
    End Function

    Public Overrides Function TrySetMember(
        ByVal binder As System.Dynamic.SetMemberBinder,
        ByVal value As Object) As Boolean

        ' Converting the property name to lowercase
        ' so that property names become case-insensitive.
        dictionary(binder.Name.ToLower()) = value

        ' You can always add a value to a dictionary,
        ' so this method always returns true.
        Return True
    End Function
End Class

Sub Main()
    ' 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"

    ' Getting values of the dynamic properties.
    ' The TryGetMember method is called.
    ' Note that property names are now case-insensitive,
    ' although they are case-sensitive in C#.
    Console.WriteLine(person.firstname & " " & person.lastname)

    ' Getting the value of the Count property.
    ' The TryGetMember is not called, 
    ' because the property is defined in the class.
    Console.WriteLine("Number of dynamic properties:" & person.Count)

    ' The following statement throws an exception at run time.
    ' There is no "address" property,
    ' so the TryGetMember method returns false and this causes
    ' a MissingMemberException.
    ' Console.WriteLine(person.address)
End Sub
' This examples has the following output:
' Ellen Adams
' Number of dynamic properties: 2

Keterangan

Kelas yang berasal dari DynamicObject kelas dapat mengambil alih metode ini untuk menentukan bagaimana operasi yang mendapatkan nilai anggota harus dilakukan untuk objek dinamis. Ketika metode tidak ditimpa, pengikat run-time bahasa menentukan perilaku. (Dalam kebanyakan kasus, pengecualian run-time dilemparkan.)

Metode ini dipanggil ketika Anda memiliki pernyataan seperti Console.WriteLine(sampleObject.SampleProperty), di mana sampleObject adalah instans kelas yang berasal dari DynamicObject kelas .

Anda juga dapat menambahkan anggota Anda sendiri ke kelas yang DynamicObject berasal dari kelas . Jika kelas Anda menentukan properti dan juga mengambil TrySetMember alih metode , runtime bahasa dinamis (DLR) terlebih dahulu menggunakan pengikat bahasa untuk mencari definisi statis properti di kelas . Jika tidak ada properti seperti itu, DLR memanggil TrySetMember metode .

Berlaku untuk