예제 #1
0
        public static void RemovePathValue(object obj, string pathExpression)
        {
            string[] segments = pathExpression.Split('.').Select(segment => segment.ToLower()).ToArray();
            dynamic  next     = obj;

            for (var i = 0; i < segments.Length - 1; i++)
            {
                next = ResolveSegment(next, segments[i], addMissing: true);
            }

            if (next != null)
            {
                var segment       = segments.Last();
                int iIndexerStart = segment.IndexOf('[');
                if (iIndexerStart > 0)
                {
                    var index = int.Parse(segment.Substring(iIndexerStart + 1).TrimEnd(']'));
                    segment     = segment.Substring(0, iIndexerStart);
                    next        = ObjectPath.GetObjectProperty(next, segment);
                    next[index] = null;
                }
                else
                {
                    try
                    {
                        next.Remove(segment);
                    }
                    catch (Exception)
                    {
                        ObjectPath.SetObjectProperty(next, segment, null);
                    }
                }
            }
        }
예제 #2
0
        public static void RemovePathValue(object obj, string path)
        {
            if (!TryResolveBracketValues(obj, ref path))
            {
                return;
            }

            string[] segments = SplitSegments(path).ToArray();
            dynamic  next     = obj;

            for (var i = 0; i < segments.Length - 1; i++)
            {
                var segment     = segments[i];
                var nextSegment = segments[i + 1];
                next = ResolveSegment(next, segment, nextSegment);

                if (next == null)
                {
                    return;
                }
            }

            if (next != null)
            {
                var lastSegment = segments.Last();
                if (IsArraySegment(lastSegment))
                {
                    var indexArgs = GetIndexArg(lastSegment);
                    if (int.TryParse(indexArgs, out int index))
                    {
                        next[index] = null;
                        return;
                    }
                    else
                    {
                        try
                        {
                            next.Remove(indexArgs);
                        }
                        catch (Exception)
                        {
                            ObjectPath.SetObjectProperty(next, indexArgs, null);
                        }
                    }
                }
                else
                {
                    try
                    {
                        next.Remove(lastSegment);
                    }
                    catch (Exception)
                    {
                        ObjectPath.SetObjectProperty(next, lastSegment, null);
                    }
                }
            }
        }