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)衍生自 類別的 DynamicObject 作業sampleObjectargs[0]等於100。

result
Object

物件引動過程的結果。

傳回

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

範例

假設您需要數據結構來儲存數位的文字和數值表示。 您想要能夠個別指定每個屬性的值,也能夠初始化單一語句中的所有屬性。

下列程式代碼範例示範 DynamicNumber 衍生自 類別的 DynamicObject 類別。 DynamicNumberTryInvoke 覆寫 方法,以一次啟用所有屬性的初始化。 它也會覆寫 TrySetMemberTryGetMember 方法,以啟用個別物件屬性的存取權。

// 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)時,會自動叫用此方法,其中 sampleObject 衍生自 DynamicObject 類別。

在 C# 中支援叫用物件的作業,但在 Visual Basic 中則不支援。 Visual Basic 編譯程式永遠不會發出程式代碼來使用此方法,而且 Visual Basic 語言不支援類似 的 sampleObject(100)語法。

適用於