DynamicObject.TryInvokeMember(InvokeMemberBinder, Object[], Object) メソッド

定義

メンバーを呼び出す演算の実装を提供します。 DynamicObject クラスの派生クラスでこのメソッドをオーバーライドして、メソッドの呼び出しなどの演算の動的な動作を指定できます。

public:
 virtual bool TryInvokeMember(System::Dynamic::InvokeMemberBinder ^ binder, cli::array <System::Object ^> ^ args, [Runtime::InteropServices::Out] System::Object ^ % result);
public virtual bool TryInvokeMember (System.Dynamic.InvokeMemberBinder binder, object[] args, out object result);
public virtual bool TryInvokeMember (System.Dynamic.InvokeMemberBinder binder, object?[]? args, out object? result);
abstract member TryInvokeMember : System.Dynamic.InvokeMemberBinder * obj[] * obj -> bool
override this.TryInvokeMember : System.Dynamic.InvokeMemberBinder * obj[] * obj -> bool
Public Overridable Function TryInvokeMember (binder As InvokeMemberBinder, args As Object(), ByRef result As Object) As Boolean

パラメーター

binder
InvokeMemberBinder

動的な演算に関する情報を提供します。 プロパティは binder.Name 、動的操作が実行されるメンバーの名前を提供します。 たとえば、 ステートメント sampleObject.SampleMethod(100)の 場合、 sampleObject は クラスから DynamicObject 派生したクラスのインスタンスであり、 binder.Name "SampleMethod" を返します。 プロパティは binder.IgnoreCase 、メンバー名で大文字と小文字を区別するかどうかを指定します。

args
Object[]

呼び出し演算でオブジェクト メンバーに渡される引数。 たとえば、 ステートメントsampleObject.SampleMethod(100)sampleObjectの 場合、 は クラスからDynamicObject派生し、 args[0] は 100 になります。

result
Object

メンバー呼び出しの結果。

戻り値

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

ディクショナリ内の値にアクセスするための代替構文を指定して、 を記述する代わりに (sampleDictionary("Text") = "Sample text" Visual Basic では ) を記述sampleDictionary["Text"] = "Sample text"sampleDictionary.Text = "Sample text"するとします。 また、このディクショナリのすべての標準辞書メソッドを呼び出せるようにする必要があります。

次のコード例では、 DynamicDictionary クラスから派生した クラスを DynamicObject 示します。 クラスには DynamicDictionary 、キーと値のペアを Dictionary<string, object> 格納する型 (Dictionary(Of String, Object) Visual Basic の場合) のオブジェクトが含まれています。 クラスのメソッドをTryInvokeMemberサポートするように メソッドをDictionary<TKey,TValue>オーバーライドし、 メソッドと TryGetMember メソッドをTrySetMemberオーバーライドして新しい構文をサポートします。 また、すべてのディクショナリ キーと値を出力する メソッドも提供 Print します。

// Add using System.Reflection;
// to the beginning of the file.

// The class derived from DynamicObject.
public class DynamicDictionary : DynamicObject
{
    // The inner dictionary.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

    // Getting a property.
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        return dictionary.TryGetValue(binder.Name, out result);
    }

    // Setting a property.
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;
        return true;
    }

    // Calling a method.
    public override bool TryInvokeMember(
        InvokeMemberBinder binder, object[] args, out object result)
    {
        Type dictType = typeof(Dictionary<string, object>);
        try
        {
            result = dictType.InvokeMember(
                         binder.Name,
                         BindingFlags.InvokeMethod,
                         null, dictionary, args);
            return true;
        }
        catch
        {
            result = null;
            return false;
        }
    }

    // This methods prints out dictionary elements.
    public void Print()
    {
        foreach (var pair in dictionary)
            Console.WriteLine(pair.Key + " " + pair.Value);
        if (dictionary.Count == 0)
            Console.WriteLine("No elements in the dictionary.");
    }
}

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";

        // Calling a method defined in the DynmaicDictionary class.
        // The Print method is called.
        person.Print();

        Console.WriteLine(
            "Removing all the elements from the dictionary.");

        // Calling a method that is not defined in the DynamicDictionary class.
        // The TryInvokeMember method is called.
        person.Clear();

        // Calling the Print method again.
        person.Print();

        // The following statement throws an exception at run time.
        // There is no Sample method
        // in the dictionary or in the DynamicDictionary class.
        // person.Sample();
    }
}

// This example has the following output:

// FirstName Ellen
// LastName Adams
// Removing all the elements from the dictionary.
// No elements in the dictionary.
' Add Imports System.Reflection
' to the beginning of the file.

' The class derived from DynamicObject.
Public Class DynamicDictionary
    Inherits DynamicObject

    ' The inner dictionary.
    Dim dictionary As New Dictionary(Of String, Object)

    ' Getting a property value.
    Public Overrides Function TryGetMember(
        ByVal binder As System.Dynamic.GetMemberBinder,
        ByRef result As Object) As Boolean

        Return dictionary.TryGetValue(binder.Name, result)
    End Function

    ' Setting a property value.
    Public Overrides Function TrySetMember(
        ByVal binder As System.Dynamic.SetMemberBinder,
        ByVal value As Object) As Boolean

        dictionary(binder.Name) = value
        Return True
    End Function


    ' Calling a method.
    Public Overrides Function TryInvokeMember(
        ByVal binder As System.Dynamic.InvokeMemberBinder,
        ByVal args() As Object, ByRef result As Object) As Boolean

        Dim dictType As Type = GetType(Dictionary(Of String, Object))
        Try
            result = dictType.InvokeMember(
                         binder.Name,
                         BindingFlags.InvokeMethod,
                         Nothing, dictionary, args)
            Return True
        Catch ex As Exception
            result = Nothing
            Return False
        End Try
    End Function

    ' This method prints out dictionary elements.
    Public Sub Print()
        For Each pair In dictionary
            Console.WriteLine(pair.Key & " " & pair.Value)
        Next
        If (dictionary.Count = 0) Then
            Console.WriteLine("No elements in the dictionary.")
        End If
    End Sub
End Class

Sub Test()
    ' 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"

    ' Calling a method defined in the DynmaicDictionary class.
    ' The Print method is called.
    person.Print()

    Console.WriteLine(
        "Removing all the elements from the dictionary.")

    ' Calling a method that is not defined in the DynamicDictionary class.
    ' The TryInvokeMember method is called.
    person.Clear()

    ' Calling the Print method again.
    person.Print()

    ' The following statement throws an exception at run time.
    ' There is no Sample method 
    ' in the dictionary or in the DynamicDictionary class.
    ' person.Sample()
End Sub


' This example has the following output:

' FirstName Ellen 
' LastName Adams
' Removing all the elements from the dictionary.
' No elements in the dictionary.

注釈

クラスから派生したクラスは、このメソッドを DynamicObject オーバーライドして、動的オブジェクトに対してオブジェクト メンバーを呼び出す操作を実行する方法を指定できます。 メソッドがオーバーライドされない場合、言語の実行時バインダーによって動作が決まります。 (ほとんどの場合、言語固有の実行時例外がスローされます)。

このメソッドがオーバーライドされると、 などの sampleObject.SampleMethod(100)操作を実行すると自動的に呼び出されます。ここで sampleObjectDynamicObject は クラスから派生します。

クラスから派生したクラスに独自のメソッドを DynamicObject 追加することもできます。 たとえば、 メソッドを TryInvokeMember オーバーライドすると、動的ディスパッチ システムは最初に、指定されたメソッドが クラスに存在するかどうかを判断しようとします。 メソッドが見つからない場合は、 実装が使用されます TryInvokeMember

このメソッドでは、 パラメーターと out パラメーターはサポートrefされていません。 配列内 args のすべてのパラメーターは値渡しされます。

適用対象