DynamicObject.TryInvoke(InvokeBinder, Object[], Object) Metoda

Definice

Poskytuje implementaci operací, které vyvolávají objekt. Třídy odvozené z DynamicObject třídy mohou přepsat tuto metodu k určení dynamického chování operací, jako je vyvolání objektu nebo delegáta.

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

Parametry

binder
InvokeBinder

Poskytuje informace o operaci vyvolání.

args
Object[]

Argumenty předané objektu během operace vyvolání. Například pro sampleObject(100) operaci, kde sampleObject je odvozena z DynamicObject třídy, args[0] je rovna 100.

result
Object

Výsledek vyvolání objektu.

Návraty

trueje-li operace úspěšná; v opačném případě . false Pokud tato metoda vrátí false, run-time binder jazyka určuje chování. (Ve většině případů se vyvolá výjimka za běhu specifická pro jazyk.

Příklady

Předpokládejme, že potřebujete datovou strukturu pro ukládání textových a číselných reprezentací čísel. Chcete mít možnost zadat hodnotu pro každou vlastnost jednotlivě a také být schopni inicializovat všechny vlastnosti v jednom příkazu.

Následující příklad kódu ukazuje DynamicNumber třídu, která je odvozena z DynamicObject třídy. DynamicNumber přepíše metodu TryInvoke pro povolení inicializace všech vlastností najednou. Přepíše TrySetMember také metody a TryGetMember povolí přístup k jednotlivým vlastnostem objektu.

// 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

Poznámky

Třídy odvozené z DynamicObject třídy mohou přepsat tuto metodu určit, jak operace, které vyvolat objekt mají být provedeny pro dynamický objekt. Pokud metoda není přepsána, pořadač za běhu jazyka určuje chování. (Ve většině případů se vyvolá výjimka za běhu.)

Pokud je tato metoda přepsána, je automaticky vyvolána, pokud máte operaci jako sampleObject(100), kde sampleObject je odvozena z DynamicObject třídy.

Operace pro vyvolání objektu je podporována v jazyce C#, ale ne v Visual Basic. Kompilátor Visual Basic nikdy nevysílá kód pro použití této metody a jazyk Visual Basic nepodporuje syntaxi, jako je sampleObject(100).

Platí pro