DynamicObject.TryInvoke Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Provides the implementation for operations that invoke an object. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations such as invoking an object or a delegate.

Namespace:  System.Dynamic
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Overridable Function TryInvoke ( _
    binder As InvokeBinder, _
    args As Object(), _
    <OutAttribute> ByRef result As Object _
) As Boolean
public virtual bool TryInvoke(
    InvokeBinder binder,
    Object[] args,
    out Object result
)

Parameters

  • args
    Type: array<System.Object[]
    The arguments that are passed to the object during the invoke operation. For example, for the sampleObject(100) operation, where sampleObject is derived from the DynamicObject class, args[0] is equal to 100.
  • result
    Type: System.Object%
    The result of the object invocation.

Return Value

Type: System.Boolean
true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.

Remarks

Classes derived from the DynamicObject class can override this method to specify how operations that invoke an object should be performed for a dynamic object. When the method is not overridden, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)

If this method is overridden, it is automatically invoked when you have an operation like sampleObject(100), where sampleObject is derived from the DynamicObject class.

The operation for invoking an object is supported in C# but not in Visual Basic. The Visual Basic compiler never emits code to use this method, and the Visual Basic language does not support syntax like sampleObject(100).

Examples

Assume that you need a data structure to store textual and numeric representations of numbers. You want to be able to specify the value for each property individually and also to be able to initialize all properties in a single statement.

The following code example demonstrates the DynamicNumber class, which is derived from the DynamicObject class. DynamicNumber overrides the TryInvoke method to enable initialization of all properties at one time. It also overrides the TrySetMember and TryGetMember methods to enable access to individual object properties.

// 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 System.Windows.Controls.TextBlock outputBlock;

   static void Demo(System.Windows.Controls.TextBlock outputBlock, 
                    string[] args)
   {
      Program.outputBlock = outputBlock;

      // 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.
      outputBlock.Text += number.Numeric + " " + number.Textual + "\n";

      // Invoking an object.
      // The TryInvoke method is called.
      number(2, "Two");
      outputBlock.Text += number.Numeric + " " + number.Textual + "\n";

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

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.