Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DynamicObject::TryInvokeMember Method

Provides the implementation for operations that invoke a member. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations such as calling a method.

Namespace:  System.Dynamic
Assemblies:   System.Dynamic.Runtime (in System.Dynamic.Runtime.dll)
  System.Core (in System.Core.dll)

public:
virtual bool TryInvokeMember(
	InvokeMemberBinder^ binder, 
	array<Object^>^ args, 
	[OutAttribute] Object^% result
)

Parameters

binder
Type: System.Dynamic::InvokeMemberBinder

Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the DynamicObject class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive.

args
Type: array<System::Object>

The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the DynamicObject class, args[0] is equal to 100.

result
Type: System::Object%

The result of the member 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.)

Classes derived from the DynamicObject class can override this method to specify how operations that invoke an object member 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 language-specific run-time exception is thrown.)

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

You can also add your own methods to classes that are derived from the DynamicObject class. For example, if you override the TryInvokeMember method, the dynamic dispatch system first attempts to determine whether the specified method exists in the class. If it does not find the method, it uses the TryInvokeMember implementation.

This method does not support ref and out parameters. All parameters in the args array are passed by value.

Assume that you want to provide alternative syntax for accessing values in a dictionary, so that instead of writing sampleDictionary["Text"] = "Sample text" (sampleDictionary("Text") = "Sample text" in Visual Basic), you can write sampleDictionary.Text = "Sample text". Also, you want to be able to call all the standard dictionary methods on this dictionary.

The following code example demonstrates the DynamicDictionary class, which is derived from the DynamicObject class. The DynamicDictionary class contains an object of the Dictionary<string, object> type (Dictionary(Of String, Object) in Visual Basic) to store the key-value pairs. It overrides the TryInvokeMember method to support methods of the Dictionary<TKey, TValue> class and overrides the TrySetMember and TryGetMember methods to support the new syntax. It also provides a Print method, which prints out all dictionary keys and values.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft