Open ds1709 opened 1 year ago
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Tagging subscribers to this area: @cston See info in area-owners.md if you want to be subscribed.
Author: | ds1709 |
---|---|
Assignees: | - |
Labels: | `area-System.Dynamic.Runtime`, `untriaged` |
Milestone: | - |
The DLR is basically fossilized now, so I wouldn't expect it to improve here. But the information you're looking for is actually present on the CSharpBinaryOperationBinder
, just hidden in an internal
property. If accessing that is acceptable for you, you could get the information you need:
private static Type cSharpBinaryOperationBinder = typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly
.GetType("Microsoft.CSharp.RuntimeBinder.CSharpBinaryOperationBinder")!;
...
if (binder.GetType() == cSharpBinaryOperationBinder)
{
bool isLogicalOperation = (bool)cSharpBinaryOperationBinder
.GetProperty("IsLogicalOperation", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(binder)!;
...
}
But the information you're looking for is actually present on the
CSharpBinaryOperationBinder
, just hidden in aninternal
property.
Thnx for information. Seems like it can work, will try.
However, it's better to be any public
way to do this.
Description
As described on MSDN, in DLR operations
AndAlso
andOrElse
represent pairs ofIsFalse/And
andIsTrue/Or
. This is becoming a problem when I'm trying to implement some zero/non-zero boolean logic.For example, I have some dynamic object over integer. Zero value is false, non-zero is true. In c++ it works like this:
With current implementation of DLR I can't implement such behavior coz when I binding binary operation, I got
And/Or
operation type instead of expectedAndAlso/OrElse
. So I dont know, was it logical or bitwise operation.Reproduction Steps
Define this simple class:
And then use it like this:
Expected behavior
Variable
c
must be instance ofMetaUInt32
over1
, becausea
andb
are non-zero values (10
and20
).Actual behavior
Variable
c
is instance ofMetaUInt32
over0
, because it's a result ofbitwise and
over10
and20
.Regression?
No response
Known Workarounds
As workaround, I can implement casting of
MetaUInt32
to boolean and then make boolean binary operation over cast results, like this:It's not too hard to do, but if your dynamic class also implements bitwise logic, you must keep in mind, that boolean binary logic works correctly only if you cast your object to boolean first.
Also it's becoming a problem when you are working with dynamic expressions (
System.Linq.Expressions.Expression.Dynamic
). If you'are implementing binary operation, you must have special case forAndAlso/OrElse
operations, because them are not supported byBinaryOperationBinder
.Configuration
No response
Other information
No response