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) 경우 여기서 sampleObject 는 클래스에서 DynamicObject 파생되며 는 args[0] 100과 같습니다.

result
Object

개체 호출의 결과입니다.

반환

작업에 성공하면 true이고, 그렇지 않으면 false입니다. 이 메서드가 false를 반환하는 경우 언어의 런타임 바인더에 따라 동작이 결정됩니다. 대부분의 경우 언어별 런타임 예외가 throw됩니다.

예제

숫자의 텍스트 및 숫자 표현을 저장하기 위해 데이터 구조가 필요하다고 가정합니다. 각 속성의 값을 개별적으로 지정하고 단일 문에서 모든 속성을 초기화할 수도 있습니다.

다음 코드 예제에서는 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 하여 동적 개체에 대해 개체를 호출하는 작업을 수행하는 방법을 지정할 수 있습니다. 메서드를 재정의하지 않으면 언어의 런타임 바인더가 동작을 결정합니다. 대부분의 경우 런타임 예외가 throw됩니다.

이 메서드가 재정의된 경우 와 같은 sampleObject(100)작업이 있을 때 자동으로 호출됩니다. 여기서 sampleObject 는 클래스에서 DynamicObject 파생됩니다.

개체 호출 작업은 C#에서 지원되지만 Visual Basic에서는 지원되지 않습니다. Visual Basic 컴파일러는 이 메서드를 사용하기 위해 코드를 내보내지 않으며 Visual Basic 언어는 와 같은 sampleObject(100)구문을 지원하지 않습니다.

적용 대상