DynamicObject.TryGetMember(GetMemberBinder, Object) Metódus

Definíció

A tagértékeket lekérő műveletek implementációját biztosítja. Az osztályból származó osztályok felülbírálhatják ezt a DynamicObject metódust, hogy dinamikus viselkedést adjanak meg az olyan műveletekhez, mint például egy tulajdonság értékének lekérése.

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

Paraméterek

binder
GetMemberBinder

Információt nyújt a dinamikus műveletnek nevezett objektumról. A binder.Name tulajdonság annak a tagnak a nevét adja meg, amelyen a dinamikus műveletet végrehajtja. Az utasítás esetében Console.WriteLine(sampleObject.SampleProperty) például, ahol sampleObject az osztály egy példánya az osztályból DynamicObject származik, binder.Name a "SampleProperty" értéket adja vissza. A binder.IgnoreCase tulajdonság megadja, hogy a tagnév megkülönbözteti-e a kis- és nagybetűket.

result
Object

A lekéréses művelet eredménye. Ha például a metódus egy tulajdonsághoz van meghívva, hozzárendelheti a tulajdonság értékét a tulajdonsághoz result.

Válaszok

trueha a művelet sikeres; egyéb esetben. false Ha ez a metódus visszatér false, a nyelv futásidejű kötése határozza meg a viselkedést. (A legtöbb esetben futásidejű kivételt eredményez.)

Példák

Tegyük fel, hogy alternatív szintaxist szeretne adni egy szótár értékeinek eléréséhez, hogy ahelyett, hogy sampleDictionary["Text"] = "Sample text" (sampleDictionary("Text") = "Sample text" Visual Basic) írna, sampleDictionary.Text = "Sample text" írhat. Ennek a szintaxisnak a kis- és nagybetűk érzéketlennek kell lenniük, így sampleDictionary.Text ez egyenértékű a következőkkel sampleDictionary.text:

Az alábbi példakód az DynamicDictionary osztályból DynamicObject származtatott osztályt mutatja be. A DynamicDictionary osztály a Dictionary<string, object> típusú (Dictionary(Of String, Object) Visual Basic) objektumot tartalmazza a kulcs-érték párok tárolásához, és felülbírálja a TrySetMember és TryGetMember metódust az új szintaxis támogatásához. Emellett egy tulajdonságot Count is biztosít, amely azt mutatja, hogy a szótár hány dinamikus tulajdonságot tartalmaz.

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

Megjegyzések

Az osztályból származtatott osztályok felülbírálhatják ezt a DynamicObject metódust, hogy megadják, hogyan kell végrehajtani a tagértékeket lekérő műveleteket egy dinamikus objektumon. Ha a metódus nincs felülírva, a nyelv futásidejű kötése határozza meg a viselkedést. (A legtöbb esetben futásidejű kivételt eredményez.)

Ezt a metódust akkor hívjuk meg, ha olyan utasításokkal rendelkezik, mint Console.WriteLine(sampleObject.SampleProperty)például az sampleObject osztály egy példánya, amely az DynamicObject osztályból származik.

Saját tagokat is hozzáadhat az DynamicObject osztályból származó osztályokhoz. Ha az osztály tulajdonságokat határoz meg, és felülbírálja a TrySetMember metódust is, a dinamikus nyelvi futtatókörnyezet (DLR) először a nyelvi kötőanyagot használja egy tulajdonság statikus definíciójának kereséséhez az osztályban. Ha nincs ilyen tulajdonság, a DLR meghívja a metódust TrySetMember .

A következőre érvényes: