共用方式為


DynamicObject.TryGetMember(GetMemberBinder, Object) 方法

定義

提供取得成員值之作業的實作。 衍生自 DynamicObject 類別的類別可以覆寫這個方法,以指定取得屬性值這類作業的動態行為。

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

參數

binder
GetMemberBinder

提供已呼叫動態作業之物件的相關資訊。 屬性 binder.Name 會提供執行動態作業的成員名稱。 例如,針對 Console.WriteLine(sampleObject.SampleProperty) 語句,其中 sampleObject 是衍生自 DynamicObject 類別的類別實例, binder.Name 會傳回 “SampleProperty”。 屬性 binder.IgnoreCase 會指定成員名稱是否區分大小寫。

result
Object

取得作業的結果。 例如,如果是針對屬性呼叫這個方法,您可以將屬性值指派給 result

傳回

如果作業成功,則為 true,否則為 false。 如果這個方法傳回 false,語言的執行階段繫結器會決定行為。 (在大部分情況下,會擲回執行階段例外狀況)。

範例

假設您想要提供替代語法來存取字典中的值,因此您不需要在 Visual Basic) 中撰寫 sampleDictionary["Text"] = "Sample text" (sampleDictionary("Text") = "Sample text" ,而是可以撰寫 sampleDictionary.Text = "Sample text"。 此外,此語法必須不區分大小寫,因此相當於 sampleDictionary.TextsampleDictionary.text

下列程式代碼範例示範 DynamicDictionary 衍生自 類別的 DynamicObject 類別。 類別 DynamicDictionary 包含 Visual Basic 中類型 (的物件 Dictionary<string, object>) , Dictionary(Of String, Object) 用來儲存機碼/值組,並覆寫 TrySetMemberTryGetMember 方法來支援新的語法。 它也會提供 Count 屬性,其中顯示字典包含多少動態屬性。

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

備註

衍生自 類別的 DynamicObject 類別可以覆寫這個方法,以指定如何為動態物件執行取得成員值的作業。 未覆寫方法時,語言的運行時間系結器會決定行為。 (在大部分情況下,會擲回執行階段例外狀況)。

當您有 之類的 Console.WriteLine(sampleObject.SampleProperty)語句時,會呼叫這個方法,其中 sampleObject 是衍生自 類別的 DynamicObject 類別實例。

您也可以將自己的成員新增至衍生自 類別的 DynamicObject 類別。 如果您的類別定義屬性並覆寫 TrySetMember 方法,動態語言執行平臺 (DLR) 會先使用語言系結器來尋找 類別中屬性的靜態定義。 如果沒有這類屬性,DLR 會呼叫 TrySetMember 方法。

適用於