Exemplo n.º 1
0
 public static void Main()
 {
     var c  = new CustomOperators();
     var c2 = +c;
     var c3 = c + c2;
     var t  = (TargetType)c3;
 }
        public static void Main()
        {
            var c          = new CustomOperators();
            var expression = typeof(System.Linq.Expressions.Expression);

            c = -c;
            var t = typeof(TargetType);
        }
Exemplo n.º 3
0
        private static string EvaluateTemplateContext(string path, IDictionary <string, object> context)
        {
            var splitted     = path.Trim('{', '}').Split(':');
            var cleanPath    = splitted.First().Replace(" ", string.Empty);
            var pathElements = new Queue <string>(cleanPath.Split('.'));

            // TODO: this can be expressed in a functional way.
            // Follow the path in the object's properties.
            context.TryGetValue(pathElements.Dequeue(), out var obj);
            while (obj != null && pathElements.Any())
            {
                var pathElement = pathElements.Dequeue();

                // Custom operators are priorized.
                if (CustomOperators.TryGetValue(pathElement, out var customOperator))
                {
                    obj = customOperator(obj);
                    continue;
                }

                obj = TryDictionaryLookup(obj, pathElement)
                      ?? obj.GetType().GetProperty(pathElement)?.GetValue(obj)
                      ?? obj.GetType().GetMethod(pathElement)?.Invoke(obj, new object[0]);
            }

            // Apply formatting if specified.
            var format = splitted.Skip(1).FirstOrDefault();

            if (format != null && Formatters.TryGetValue(format.Trim(), out var formatter))
            {
                obj = formatter(obj);
            }

            // Special formatter: Apply string value without quotes into template.
            if ("rawstring".Equals(format, StringComparison.OrdinalIgnoreCase) &&
                obj is string stringValue)
            {
                return(stringValue);
            }

            return(JsonConvert.SerializeObject(obj));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Adds an operator to the expression evaluation system
 /// </summary>
 /// <param name="Operator"></param>
 public void AddCustomOperator(CustomExpressionOperator Operator)
 {
     CustomOperators.Add(Operator);
 }