DynamicObject.TryGetMember(GetMemberBinder, Object) Metoda

Definice

Poskytuje implementaci pro operace, které získávají členské hodnoty. Třídy odvozené z DynamicObject třídy mohou přepsat tuto metodu k určení dynamického chování pro operace, jako je získání hodnoty pro vlastnost.

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

Parametry

binder
GetMemberBinder

Poskytuje informace o objektu, který volal dynamickou operaci. Vlastnost binder.Name poskytuje název člena, se kterým se dynamická operace provádí. Například pro Console.WriteLine(sampleObject.SampleProperty) příkaz , kde sampleObject je instance třídy odvozené z DynamicObject třídy , binder.Name vrátí "SampleProperty". Vlastnost binder.IgnoreCase určuje, zda název členu rozlišuje velká a malá písmena.

result
Object

Výsledek operace get. Pokud je například volána metoda pro vlastnost, můžete přiřadit hodnotu vlastnosti k result.

Návraty

truepokud je operace úspěšná; v opačném případě . false Pokud tato metoda vrátí false, pořadač běhu jazyka určuje chování. (Ve většině případů se vyvolá výjimka za běhu.)

Příklady

Předpokládejme, že chcete poskytnout alternativní syntaxi pro přístup k hodnotám ve slovníku, takže místo psaní sampleDictionary["Text"] = "Sample text" (sampleDictionary("Text") = "Sample text" v jazyce Visual Basic) můžete psát sampleDictionary.Text = "Sample text". Tato syntaxe také musí rozlišovat malá a velká písmena, takže sampleDictionary.Text je ekvivalentní k sampleDictionary.text.

Následující příklad kódu ukazuje DynamicDictionary třídu, která je odvozena z DynamicObject třídy . Třída DynamicDictionary obsahuje objekt Dictionary<string, object> typu (Dictionary(Of String, Object) v jazyce Visual Basic) pro uložení párů klíč-hodnota a přepíše TrySetMember metody a TryGetMember pro podporu nové syntaxe. Poskytuje také Count vlastnost, která ukazuje, kolik dynamických vlastností slovník obsahuje.

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

Poznámky

Třídy odvozené z DynamicObject třídy mohou přepsat tuto metodu k určení způsobu provádění operací, které získávají hodnoty členů pro dynamický objekt. Pokud metoda není přepsána, pořadač za běhu jazyka určuje chování. (Ve většině případů se vyvolá výjimka za běhu.)

Tato metoda je volána, pokud máte příkazy jako Console.WriteLine(sampleObject.SampleProperty), kde sampleObject je instance třídy odvozená z DynamicObject třídy .

Můžete také přidat vlastní členy do tříd odvozených z DynamicObject třídy . Pokud vaše třída definuje vlastnosti a také přepisuje metodu TrySetMember , modul runtime dynamického jazyka (DLR) nejprve použije jazykový pořadač k vyhledání statické definice vlastnosti ve třídě . Pokud taková vlastnost neexistuje, dlr volá metodu TrySetMember .

Platí pro