DynamicObject.TryInvoke(InvokeBinder, Object[], Object) メソッド

定義

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

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

パラメーター

binder
InvokeBinder

呼び出し演算に関する情報を提供します。

args
Object[]

呼び出し演算でオブジェクトに渡される引数。 たとえば、 sampleObject(100) が クラスから派生した 操作 sampleObjectDynamicObject 場合、 args[0] は 100 と等しくなります。

result
Object

オブジェクト呼び出しの結果。

戻り値

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

数値のテキスト表現と数値表現を格納するためのデータ構造が必要であるとします。 各プロパティの値を個別に指定し、1 つのステートメントですべてのプロパティを初期化できるようにする必要があります。

次のコード例では、 DynamicNumber クラスから派生した クラスを DynamicObject 示します。 DynamicNumber は、 メソッドを TryInvoke オーバーライドして、すべてのプロパティの初期化を一度に有効にします。 また、 メソッドと TryGetMember メソッドをTrySetMemberオーバーライドして、個々のオブジェクト プロパティへのアクセスを有効にします。

// The class derived from DynamicObject.
public class DynamicNumber : DynamicObject
{
    // The inner dictionary to store field names and values.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

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

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

    // Initializing properties with arguments' values.
    public override bool TryInvoke(
        InvokeBinder binder, object[] args, out object result)
    {
        // The invoke operation in this case takes two arguments.
        // The first one is integer and the second one is string.
        if ((args.Length == 2) &&
            (args[0].GetType() == typeof(int)) &&
            (args[1].GetType() == typeof(String)))
        {
            // If the property already exists,
            // its value is changed.
            // Otherwise, a new property is created.
            if (dictionary.ContainsKey("Numeric"))
                dictionary["Numeric"] = args[0];
            else
                dictionary.Add("Numeric", args[0]);

            if (dictionary.ContainsKey("Textual"))
                dictionary["Textual"] = args[1];
            else
                dictionary.Add("Textual", args[1]);

            result = true;
            return true;
        }

        else
        {
            // If the number of arguments is wrong,
            // or if arguments are of the wrong type,
            // the method returns false.
            result = false;
            return false;
        }
    }
}
class Program
{
    static void Test(string[] args)
    {
        // Creating a dynamic object.
        dynamic number = new DynamicNumber();

        // Adding and initializing properties.
        // The TrySetMember method is called.
        number.Numeric = 1;
        number.Textual = "One";

        // Printing out the result.
        // The TryGetMember method is called.
        Console.WriteLine(number.Numeric + " " + number.Textual);

        // Invoking an object.
        // The TryInvoke method is called.
        number(2, "Two");
        Console.WriteLine(number.Numeric + " " + number.Textual);

        // The following statement produces a run-time exception
        // because in this example the method invocation
        // expects two arguments.
        // number(0);
    }
}

// This code example produces the following output:

// 1 One
// 2 Two

注釈

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

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

オブジェクトを呼び出す操作は C# ではサポートされていますが、Visual Basic ではサポートされていません。 Visual Basic コンパイラでは、このメソッドを使用するコードは出力されず、Visual Basic 言語では のような sampleObject(100)構文はサポートされていません。

適用対象