DynamicObject.TrySetMember(SetMemberBinder, Object) Metoda

Definice

Poskytuje implementaci operací, které nastavily hodnoty členů. Třídy odvozené z DynamicObject třídy mohou přepsat tuto metodu určit dynamické chování operací, jako je nastavení hodnoty pro vlastnost.

public:
 virtual bool TrySetMember(System::Dynamic::SetMemberBinder ^ binder, System::Object ^ value);
public virtual bool TrySetMember(System.Dynamic.SetMemberBinder binder, object value);
public virtual bool TrySetMember(System.Dynamic.SetMemberBinder binder, object? value);
abstract member TrySetMember : System.Dynamic.SetMemberBinder * obj -> bool
override this.TrySetMember : System.Dynamic.SetMemberBinder * obj -> bool
Public Overridable Function TrySetMember (binder As SetMemberBinder, value As Object) As Boolean

Parametry

binder
SetMemberBinder

Poskytuje informace o objektu, který volal dynamickou operaci. Vlastnost binder.Name poskytuje název člena, ke kterému je hodnota přiřazena. Například pro příkaz sampleObject.SampleProperty = "Test", kde sampleObject je instance třídy odvozené z DynamicObject třídy, binder.Name vrátí "SampleProperty". Vlastnost binder.IgnoreCase určuje, zda je název členu rozlišovat velká a malá písmena.

value
Object

Hodnota, která se má nastavit na člena. Například pro sampleObject.SampleProperty = "Test", kde sampleObject je instance třídy odvozené z DynamicObject třídy, value je "Test".

Návraty

trueje-li operace úspěšná; v opačném případě . false Pokud tato metoda vrátí false, run-time binder jazyka určuje chování. (Ve většině případů se vyvolá výjimka za běhu specifická pro jazyk.)

Příklady

Předpokládejme, že chcete zadat 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 Visual Basic) můžete psát sampleDictionary.Text = "Sample text". Tato syntaxe také musí být nerozlišována malá a velká písmena, takže je sampleDictionary.Text ekvivalentní 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 typu Dictionary<string, object> (Dictionary(Of String, Object) v Visual Basic) pro uložení párů klíč-hodnota a přepisuje metody TrySetMember 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 určit, jak operace, které nastavují hodnotu na člena mají být provedeny 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 specifická pro jazyk.)

Tato metoda je volána, pokud máte příkazy jako sampleObject.SampleProperty = "Test", kde sampleObject je instance třídy, která je odvozena z DynamicObject třídy.

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

Platí pro