DynamicObject.TryGetMember(GetMemberBinder, Object) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Poskytuje implementaci operací, které získávají hodnoty členů. Třídy odvozené z DynamicObject třídy mohou přepsat tuto metodu určit dynamické chování operací, 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, na kterém je dynamická operace provedena. 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 je název členu rozlišovat 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 resultvlastnosti .
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.)
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é získat členské hodnoty 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.)
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.
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 .