Esempio n. 1
0
        public void Add(CustomExpression expression)
        {
            if (_expressionMap.ContainsKey(expression.Name.ToLower()))
            {
                throw new ArgumentException($"Function Name '{expression.Name}' is already present in the collection.");
            }

            if (_expressionMap.Values.Any(expr => expr.RegexPattern.Equals(expression.RegexPattern)))
            {
                throw new ArgumentException(
                          $"Regular Expression '{expression.RegexPattern}' is already present in the collection.");
            }

            _expressionMap.Add(expression.Name.ToLower(), expression);
        }
Esempio n. 2
0
        private static string ProcessRegexMatch(object targetObject, string newStr, Match match, CustomExpression customExpr, int originalLength,
                                                ref int lengthOffset)
        {
            var len        = match.Index + lengthOffset + match.Length;
            var firstPart  = newStr.Substring(0, match.Index + lengthOffset);
            var secondPart = newStr.Substring(len, newStr.Length - len);

            string updatedStr = string.Empty;

            if (customExpr.ReplacementFunction != null)
            {
                updatedStr += firstPart + customExpr.ReplacementFunction.Invoke(match) + secondPart;
            }
            if (customExpr.ReplacementReference != null && targetObject != null)
            {
                var method = targetObject.GetType().GetMethod(customExpr.ReplacementReference.Item1);
                var prop   = targetObject.GetType().GetProperty(customExpr.ReplacementReference.Item1);

                if (method != null && prop == null)
                {
                    if (customExpr.ReplacementReference.Item2 != null)
                    {
                        updatedStr += firstPart + method.Invoke(targetObject, customExpr.ReplacementReference.Item2.ToArray()) + secondPart;
                    }
                    else
                    {
                        updatedStr += firstPart + method.Invoke(targetObject, new object[] { }) + secondPart;
                    }
                }

                if (method == null && prop != null)
                {
                    updatedStr += firstPart + prop.GetValue(targetObject) + secondPart;
                }

                if (method == null && prop == null)
                {
                    throw new ArgumentNullException($"{customExpr.Name} could not be found on object {targetObject.GetType().FullName}");
                }
            }

            lengthOffset = updatedStr.Length - originalLength;
            return(updatedStr);
        }