/// <summary>
 /// Visit a parse tree produced by the <c>identifierExpression</c>
 /// labeled alternative in <see cref="AntlrConditionParser.expression"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitIdentifierExpression([NotNull] AntlrConditionParser.IdentifierExpressionContext context)
 {
     return(VisitChildren(context));
 }
 /// <summary>
 /// Exit a parse tree produced by the <c>identifierExpression</c>
 /// labeled alternative in <see cref="AntlrConditionParser.expression"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitIdentifierExpression([NotNull] AntlrConditionParser.IdentifierExpressionContext context)
 {
 }
Esempio n. 3
0
        public override Result VisitIdentifierExpression(AntlrConditionParser.IdentifierExpressionContext context)
        {
            var identifier = context.IDENTIFIER().GetText();

            var obj = _param;

            foreach (var propName in identifier.Split('.'))
            {
                var prop = obj.GetType().GetRuntimeProperty(propName);

                if (prop == null)
                {
                    prop = obj.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, propName, StringComparison.InvariantCultureIgnoreCase));
                }

                if (prop == null)
                {
                    var customField = obj.GetType().GetProperty("CustomFields");

                    if (customField == null)
                    {
                        throw new Exception($"Property {identifier} not found");
                    }

                    var custom = (IDictionary <string, object>)customField.GetValue(obj);

                    if (!custom.ContainsKey(propName))
                    {
                        throw new Exception($"Property {identifier} not found");
                    }

                    obj = custom[propName];
                }
                else
                {
                    obj = prop.GetValue(obj);
                }
            }

            var result = new Result();

            switch (obj)
            {
            case int i:
                result.Decimal = i;
                break;

            case uint ui:
                result.Decimal = ui;
                break;

            case short s:
                result.Decimal = s;
                break;

            case ushort us:
                result.Decimal = us;
                break;

            case byte b:
                result.Decimal = b;
                break;

            case sbyte sb:
                result.Decimal = sb;
                break;

            case long l:
                result.Decimal = l;
                break;

            case ulong ul:
                result.Decimal = ul;
                break;

            case float fl:
                result.Decimal = (decimal?)fl;
                break;

            case double db:
                result.Decimal = (decimal?)db;
                break;

            case bool bo:
                result.Bool = bo;
                break;

            case string str:
                result.String = str;
                break;

            default:
                result.Object = obj;
                break;
            }

            return(result);
        }