Aracılığıyla paylaş


DynamicObject.TryGetMember(GetMemberBinder, Object) Yöntem

Tanım

Üye değerlerini alan işlemler için uygulamayı sağlar. sınıfından DynamicObject türetilen sınıflar, bir özellik için değer alma gibi işlemler için dinamik davranış belirtmek üzere bu yöntemi geçersiz kılabilir.

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

Parametreler

binder
GetMemberBinder

Dinamik işlemi çağıran nesne hakkında bilgi sağlar. özelliği, binder.Name dinamik işlemin gerçekleştirildiği üyenin adını sağlar. Örneğin, sampleObject sınıfından Console.WriteLine(sampleObject.SampleProperty) türetilen DynamicObjectbinder.Name sınıfın bir örneği olan deyimi için "SampleProperty" döndürür. özelliği, binder.IgnoreCase üye adının büyük/küçük harfe duyarlı olup olmadığını belirtir.

result
Object

Get işleminin sonucu. Örneğin, bir özellik için yöntemi çağrılırsa, özellik değerini öğesine resultatayabilirsiniz.

Döndürülenler

true işlem başarılı olursa; aksi takdirde , false. Bu yöntem döndürürse false, davranışı dilin çalışma zamanı bağlayıcısı belirler. (Çoğu durumda, bir çalışma zamanı özel durumu oluşturulur.)

Örnekler

Bir sözlükteki değerlere erişmek için alternatif söz dizimi sağlamak istediğinizi varsayalım; böylece yazmak sampleDictionary["Text"] = "Sample text" yerine (sampleDictionary("Text") = "Sample text" Visual Basic'te) yazabilirsiniz sampleDictionary.Text = "Sample text". Ayrıca, bu söz dizimi büyük/küçük harfe duyarlı olmamalıdır, bu sampleDictionary.Text nedenle ile eşdeğerdir sampleDictionary.text.

Aşağıdaki kod örneği, sınıfından DynamicDictionary türetilen DynamicObject sınıfını gösterir. DynamicDictionary sınıfı, anahtar-değer çiftlerini depolamak için (Dictionary(Of String, Object) Visual Basic'te) türünde bir nesne Dictionary<string, object> içerir ve yeni söz dizimini TrySetMember desteklemek için ve TryGetMember yöntemlerini geçersiz kılar. Ayrıca, sözlüğün kaç dinamik özellik içerdiğini gösteren bir Count özellik de sağlar.

// 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

Açıklamalar

sınıfından DynamicObject türetilen sınıflar, üye değerlerini alan işlemlerin dinamik bir nesne için nasıl gerçekleştirileceğini belirtmek için bu yöntemi geçersiz kılabilir. Yöntem geçersiz kılınmadığında, davranışı dilin çalışma zamanı bağlayıcısı belirler. (Çoğu durumda, bir çalışma zamanı özel durumu oluşturulur.)

gibi Console.WriteLine(sampleObject.SampleProperty)deyimleriniz olduğunda bu yöntem çağrılır. Burada sampleObject sınıfından türetilen DynamicObject sınıfın bir örneğidir.

Ayrıca sınıfından DynamicObject türetilen sınıflara kendi üyelerinizi de ekleyebilirsiniz. Sınıfınız özellikleri tanımlar ve yöntemini de geçersiz kılarsa TrySetMember , dinamik dil çalışma zamanı (DLR) önce dil bağlayıcısını kullanarak sınıfındaki bir özelliğin statik tanımını arar. Böyle bir özellik yoksa, DLR yöntemini çağırır TrySetMember .

Şunlara uygulanır