DynamicObject.TryUnaryOperation Method (UnaryOperationBinder, Object)
Provides implementation for unary operations. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations such as negation, increment, or decrement.
Assembly: System.Core (in System.Core.dll)
abstract TryUnaryOperation : binder:UnaryOperationBinder * result:Object byref -> bool override TryUnaryOperation : binder:UnaryOperationBinder * result:Object byref -> bool
Provides information about the unary operation. The binder.Operation property returns an ExpressionType object. For example, for the negativeNumber = -number statement, where number is derived from the DynamicObject class, binder.Operation returns "Negate".
The result of the unary operation.
Return ValueType: 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 unary operations 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.)
This method is called when you have unary operations such as negation, increment, or decrement. For example, if the negativeNumber = -number, where number is derived from the DynamicObject class.method is overridden, this method is automatically invoked for statements like
You can get information about the type of the unary operation by using the Operation property of the binder parameter.
If your dynamic object is used only in C# and Visual Basic, the binder.Operation property can have one of the following values from the ExpressionType enumeration. However, in other languages such as IronPython or IronRuby, you can have other values. For more information about ExpressionType, see documentation on the CodePlex Web site.
A unary decrement operation.
A unary increment operation.
An arithmetic negation.
A logical negation.
A ones complement.
A false condition value.
a && b
A true condition value.
a || b
A unary plus.
To implement OrElse (a || b) and AndAlso (a && b) operations for dynamic objects in C#, you may want to implement both the method and the TryBinaryOperation method.
The OrElse operation consists of the unary IsTrue operation and the binary Or operation. The Or operation is performed only if the result of the IsTrue operation is false.
The AndAlso operation consists of the unary IsFalse operation and the binary And operation. The And operation is performed only if the result of the IsFalse operation is false.
Assume that you need a data structure to store textual and numeric representations of numbers, and you want to define a mathematical negation operation for such data.
The following code example demonstrates the DynamicNumber class, which is derived from the DynamicObject class. DynamicNumber overrides the method to enable the mathematical negation operation. Is also overrides the TrySetMember and TryGetMember methods to enable access to the elements.
In this example, only the mathematical negation operation is supported. If you try to write a statement like negativeNumber = +number, a run-time exception occurs.
Available since 4.5
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Available since 8.1