Skip to content

Commit

Permalink
Update operator type mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed May 30, 2023
1 parent 6435bdf commit eaf8772
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions JLChnToZ.CommonUtils.Dynamic/Limitless.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public override bool TryInvoke(InvokeBinder binder, object[] args, out object re
}

public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result) =>
typeInfo.TryInvoke(target, $"op_{binder.Operation}", new[] { arg }, out result) ||
typeInfo.TryInvoke(null, binder.Operation.ToOperatorMethodName(), new[] { target, arg }, out result) ||
(target is DynamicObject dynamicObject && dynamicObject.TryBinaryOperation(binder, arg, out result));

public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result) =>
typeInfo.TryInvoke(target, $"op_{binder.Operation}", null, out result) ||
typeInfo.TryInvoke(null, binder.Operation.ToOperatorMethodName(), new[] { target }, out result) ||
(target is DynamicObject dynamicObject && dynamicObject.TryUnaryOperation(binder, out result));

public override bool TryConvert(ConvertBinder binder, out object result) {
Expand Down
28 changes: 28 additions & 0 deletions JLChnToZ.CommonUtils.Dynamic/Utilites.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq.Expressions;
using System.Reflection;

namespace JLChnToZ.CommonUtils.Dynamic {
Expand Down Expand Up @@ -57,6 +58,33 @@ public static Type GetUndelyType(object obj) {
}

public static Type GetParameterType(ParameterInfo parameterInfo) => parameterInfo.ParameterType;

public static string ToOperatorMethodName(this ExpressionType expressionType) {
switch (expressionType) {
case ExpressionType.Equal : return "op_Equality";
case ExpressionType.NotEqual : return "op_Inequality";
case ExpressionType.GreaterThan : return "op_GreaterThan";
case ExpressionType.LessThan : return "op_LessThan";
case ExpressionType.GreaterThanOrEqual: return "op_GreaterThanOrEqual";
case ExpressionType.LessThanOrEqual : return "op_LessThanOrEqual";
case ExpressionType.Add : return "op_Addition";
case ExpressionType.Subtract : return "op_Subtraction";
case ExpressionType.Multiply : return "op_Multiply";
case ExpressionType.Divide : return "op_Division";
case ExpressionType.Modulo : return "op_Modulus";
case ExpressionType.And : return "op_BitwiseAnd";
case ExpressionType.Or : return "op_BitwiseOr";
case ExpressionType.ExclusiveOr : return "op_ExclusiveOr";
case ExpressionType.LeftShift : return "op_LeftShift";
case ExpressionType.RightShift : return "op_RightShift";
case ExpressionType.Negate : return "op_UnaryNegation";
case ExpressionType.Not : return "op_LogicalNot";
case ExpressionType.OnesComplement : return "op_OnesComplement";
case ExpressionType.Increment : return "op_Increment";
case ExpressionType.Decrement : return "op_Decrement";
default: throw new NotSupportedException();
}
}
}

internal enum MethodMatchLevel {
Expand Down

0 comments on commit eaf8772

Please sign in to comment.