Udostępnij przez


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

Definicja

Zapewnia implementację operacji, które wywołują obiekt. Klasy pochodzące z DynamicObject klasy mogą zastąpić tę metodę w celu określenia dynamicznego zachowania operacji, takich jak wywoływanie obiektu lub delegata.

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

Zawiera informacje o operacji wywołania.

args
Object[]

Argumenty przekazywane do obiektu podczas operacji wywołania. Na przykład dla sampleObject(100) operacji, gdzie sampleObject pochodzi z DynamicObject klasy, args[0] jest równa 100.

result
Object

Wynik wywołania obiektu.

Zwraca

true jeśli operacja zakończy się pomyślnie; w przeciwnym razie , false. Jeśli ta metoda zwróci falsewartość , powiązanie czasu wykonywania języka określa zachowanie. (W większości przypadków zgłaszany jest wyjątek czasu wykonywania specyficzny dla języka.

Przykłady

Załóżmy, że potrzebujesz struktury danych do przechowywania tekstowych i liczbowych reprezentacji liczb. Chcesz mieć możliwość określenia wartości dla każdej właściwości indywidualnie, a także zainicjować wszystkie właściwości w jednej instrukcji.

Poniższy przykład kodu przedstawia klasę DynamicNumber , która pochodzi z DynamicObject klasy. DynamicNumber zastępuje metodę w celu włączenia TryInvoke inicjowania wszystkich właściwości jednocześnie. Zastępuje również TrySetMember metody i TryGetMember , aby umożliwić dostęp do poszczególnych właściwości obiektu.

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

Uwagi

Klasy pochodzące z DynamicObject klasy mogą zastąpić tę metodę, aby określić sposób wykonywania operacji wywołujących obiekt dla obiektu dynamicznego. Gdy metoda nie jest zastępowana, powiązanie czasu wykonywania języka określa zachowanie. (W większości przypadków zgłaszany jest wyjątek czasu wykonywania).

Jeśli ta metoda jest zastępowana, jest ona automatycznie wywoływana, gdy masz operację taką jak sampleObject(100), gdzie sampleObject pochodzi z DynamicObject klasy .

Operacja wywoływania obiektu jest obsługiwana w języku C#, ale nie w Visual Basic. Kompilator języka Visual Basic nigdy nie emituje kodu do użycia tej metody, a język Visual Basic nie obsługuje składni, takiej jak sampleObject(100).

Dotyczy