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 を返す場合、言語のランタイム バインダーによって動作が決まります (ほとんどの場合、実行時例外がスローされます)。

ディクショナリ内の値にアクセスするための代替構文を指定して、 を記述 sampleDictionary["Text"] = "Sample text" する代わりに (sampleDictionary("Text") = "Sample text" を Visual Basic で) 記述 sampleDictionary.Text = "Sample text"できるようにするとします。 また、この構文では大文字と小文字を区別しない必要があるため sampleDictionary.Text 、 は と sampleDictionary.text同じです。

次のコード例では、 DynamicDictionary クラスから派生した クラスを DynamicObject 示します。 クラスにはDynamicDictionary、キーと値のDictionary<string, object>ペアを格納する型 (Dictionary(Of String, Object)Visual Basic では) のオブジェクトが含まれており、 メソッドと TryGetMember メソッドをTrySetMemberオーバーライドして新しい構文をサポートします。 また、ディクショナリに 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 呼び出します。

適用対象