DynamicObject.TrySetMember(SetMemberBinder, Object) メソッド

定義

メンバー値を設定する演算の実装を提供します。 DynamicObject クラスの派生クラスでこのメソッドをオーバーライドして、プロパティ値の設定などの演算の動的な動作を指定できます。

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

パラメーター

binder
SetMemberBinder

動的演算を呼び出したオブジェクトに関する情報を提供します。 binder.Name プロパティは、値の割り当て先のメンバーの名前を提供します。 たとえば、ステートメント sampleObject.SampleProperty = "Test" の場合 (sampleObjectDynamicObject クラスの派生クラスのインスタンス)、binder.Name は "SampleProperty" を返します。 メンバー名で大文字と小文字を区別するかどうかを binder.IgnoreCase プロパティで指定します。

value
Object

メンバーに設定する値。 たとえば、sampleObject.SampleProperty = "Test" の場合 (sampleObjectDynamicObject クラスの派生クラスのインスタンス)、value は "Test" となります。

戻り値

Boolean

操作が正常に終了した場合は true。それ以外の場合は false。 このメソッドが false を返す場合、言語のランタイム バインダーによって動作が決まります (ほとんどの場合、言語固有の実行時例外がスローされます)。

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

次のコード例は、クラスから派生したクラスをDynamicObject示していますDynamicDictionary。 このDynamicDictionaryクラスには、キーと値のペアを格納する型 (Dictionary<string, object>``Dictionary(Of String, Object)Visual Basic) のオブジェクトが含まれており、新しい構文をTrySetMemberサポートするメソッドとTryGetMemberメソッドがオーバーライドされます。 また、ディクショナリに 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 オーバーライドして、動的オブジェクトに対して値をメンバーに設定する操作を実行する方法を指定できます。 メソッドがオーバーライドされない場合、言語のランタイム バインダーによって動作が決定されます。 (ほとんどの場合、言語固有の実行時例外がスローされます)。

このメソッドは、クラスから派生したクラスのインスタンスのようなステートメントsampleObject.SampleProperty = "Test"``sampleObjectがある場合にDynamicObject呼び出されます。

クラスから派生したクラスに独自のメンバーを DynamicObject 追加することもできます。 クラスがプロパティを定義し、メソッドをオーバーライド TrySetMember する場合、動的言語ランタイム (DLR) はまず言語バインダーを使用して、クラス内のプロパティの静的定義を検索します。 このようなプロパティがない場合、DLR はメソッドを TrySetMember 呼び出します。

適用対象