TryBinaryOperation Method

DynamicObject::TryBinaryOperation Method (BinaryOperationBinder^, Object^, Object^%)

 

Provides implementation for binary operations. Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations such as addition and multiplication.

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

public:
virtual bool TryBinaryOperation(
	BinaryOperationBinder^ binder,
	Object^ arg,
	[OutAttribute] Object^% result
)

Parameters

binder
Type: System.Dynamic::BinaryOperationBinder^

Provides information about the binary operation. The binder.Operation property returns an ExpressionType object. For example, for the sum = first + second statement, where first and second are derived from the DynamicObject class, binder.Operation returns ExpressionType.Add.

arg
Type: System::Object^

The right operand for the binary operation. For example, for the sum = first + second statement, where first and second are derived from the DynamicObject class, arg is equal to second.

result
Type: System::Object^%

The result of the binary operation.

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 binary 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 binary operations such as addition or multiplication. For example, if the TryBinaryOperation method is overridden, it is automatically invoked for statements like sum = first + second or multiply = first*second, where first is derived from the DynamicObject class.

You can get information about the type of the binary 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.

Value

Description

C#

Visual Basic

Add

An addition operation without overflow checking, for numeric operands.

a + b

a + b

AddAssign

An addition compound assignment operation without overflow checking, for numeric operands.

a += b

Not supported.

And

A bitwise AND operation.

a & b

a And b

AndAssign

A bitwise AND compound assignment operation.

a &= b

Not supported.

Divide

An arithmetic division operation.

a / b

a / b

DivideAssign

An arithmetic division compound assignment operation.

a /= b

Not supported.

ExclusiveOr

A bitwise XOR operation.

a ^ b

a Xor b

ExclusiveOrAssign

A bitwise XOR compound assignment operation.

a ^= b

Not supported.

GreaterThan

A "greater than" comparison.

a > b

a > b

GreaterThanOrEqual

A "greater than or equal to" comparison.

a >= b

Not supported.

LeftShift

A bitwise left-shift operation.

a << b

a << b

LeftShiftAssign

A bitwise left-shift compound assignment operation.

a <<= b

Not supported.

LessThan

A "less than" comparison.

a < b

a < b

LessThanOrEqual

A "less than or equal to" comparison.

a <= b

Not supported.

Modulo

An arithmetic remainder operation.

a % b

a Mod b

ModuloAssign

An arithmetic remainder compound assignment operation.

a %= b

Not supported.

Multiply

A multiplication operation without overflow checking, for numeric operands.

a * b

a * b

MultiplyAssign

A multiplication compound assignment operation without overflow checking, for numeric operands.

a *= b

Not supported.

NotEqual

An inequality comparison.

a != b

a <> b

Or

A bitwise or logical OR operation.

a | b

a Or b

OrAssign

A bitwise or logical OR compound assignment.

a |= b

Not supported.

Power

A mathematical operation of raising a number to a power.

Not supported.

a ^ b

RightShift

A bitwise right-shift operation.

a >> b

a >> b

RightShiftAssign

A bitwise right-shift compound assignment operation.

a >>= b

Not supported.

Subtract

A subtraction operation without overflow checking, for numeric operands.

a - b

a - b

SubtractAssign

A subtraction compound assignment operation without overflow checking, for numeric operands.

a -= b

Not supported.

System_CAPS_noteNote

To implement OrElse (a || b) and AndAlso (a && b) operations for dynamic objects in C#, you may want to implement both the TryUnaryOperation 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 basic mathematical operations such as addition and subtraction for such data.

The following code example demonstrates the DynamicNumber class, which is derived from the DynamicObject class. DynamicNumber overrides the TryBinaryOperation method to enable mathematical operations. It also overrides the TrySetMember and TryGetMember methods to enable access to the elements.

In this example, only addition and subtraction operations are supported. If you try to write a statement like resultNumber = firstNumber*secondNumber, a run-time exception is thrown.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft