Exemplo n.º 1
0
        /// <summary>
        ///     Executes the specified source.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>CaptureResults.</returns>
        public CaptureResults Execute(object source)
        {
            var retVal = new CaptureResults();

            Process(String.Empty, source, retVal);
            return(retVal);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Processes the specified root.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <param name="source">The source.</param>
        /// <param name="results">The results.</param>
        private void Process(string root, object source, CaptureResults results)
        {
            if (r_Context.Visited(source))
            {
                return;
            }
            int nest = Interlocked.Increment(ref m_NestingLevel);

            try
            {
                if (nest > 25)
                {
                    Console.WriteLine("Maximum Obhject Graph Depth Reached!!!");
                    return;
                }
                r_Context.Add(source);
                Dictionary <string, FieldInfo> fields = GetAllFields(source.GetType());
                foreach (FieldInfo field in fields.Values)
                {
                    string path         = root + "." + field.Name + "{" + field.DeclaringType + "}";
                    Type   declaredType = field.FieldType;
                    object value        = field.GetValue(source);
                    if ((field.FieldType.IsValueType) || field.FieldType == typeof(string))
                    {
                        results.Register(new ValueElement(path, declaredType, value));
                    }
                    else
                    {
                        results.Register(new ReferenceElement(path, declaredType, value));
                    }
                    string safeFullName = source.GetType().FullName ?? "";
                    if (!TerminalTypes.Contains(safeFullName))
                    {
                        if ((value != null) && (field.FieldType != typeof(string)))
                        {
                            if (!field.FieldType.IsPrimitive)
                            {
                                Process(path, value, results);
                            }
                        }
                    }
                }
                Dictionary <string, EventInfo> events = GetAllEventss(source.GetType());
                results.Register(source, events);
            }
            finally
            {
                Interlocked.Decrement(ref m_NestingLevel);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Executes the specified initial graph.
        /// </summary>
        /// <param name="initialGraph">The initial graph.</param>
        /// <param name="finalGraph">The final graph.</param>
        /// <returns>ChangeList.</returns>
        /// <exception cref="System.Exception"></exception>
        private ChangeList Execute(CaptureResults initialGraph, CaptureResults finalGraph)
        {
            var retVal             = new ChangeList();
            var unusedOriginalKeys = new List <string>(initialGraph.Elements.Keys);
            var unusedFinalKeys    = new List <string>(finalGraph.Elements.Keys);

            foreach (KeyValuePair <string, Element> pair in initialGraph.Elements)
            {
                Element originalElement = pair.Value;
                Element finalElement;
                if (finalGraph.Elements.TryGetValue(pair.Key, out finalElement))
                {
                    unusedOriginalKeys.Remove(pair.Key);
                    unusedFinalKeys.Remove(pair.Key);
                    if (originalElement.GetType() != finalElement.GetType())
                    {
                        throw new Exception();
                    }
                    if (originalElement is ObjectGraphCapture.ReferenceElement)
                    {
                        if (originalElement.DeclaredType.IsArray)
                        {
                            ArrayCompare(retVal, (ObjectGraphCapture.ReferenceElement)originalElement, (ObjectGraphCapture.ReferenceElement)finalElement);
                        }
                        else
                        {
                            Compare(retVal, (ObjectGraphCapture.ReferenceElement)originalElement, (ObjectGraphCapture.ReferenceElement)finalElement);
                        }
                    }
                    else
                    {
                        Compare(retVal, (ObjectGraphCapture.ValueElement)originalElement, (ObjectGraphCapture.ValueElement)finalElement);
                    }
                }
            }
            foreach (string key in unusedOriginalKeys)
            {
                Element item = initialGraph.Elements[key];
                var     diff = new MissingGraphChange
                {
                    Path         = item.Path,
                    DeclaredType = item.DeclaredType,
                };
                var asValueChange = item as ObjectGraphCapture.ValueElement;
                if (asValueChange != null)
                {
                    diff.OldValue = asValueChange.Value;
                }
                retVal.Add(diff);
            }
            foreach (string key in unusedFinalKeys)
            {
                Element item = finalGraph.Elements[key];
                var     diff = new AddedGraphChange
                {
                    Path         = item.Path,
                    DeclaredType = item.DeclaredType,
                };
                var asValueChange = item as ObjectGraphCapture.ValueElement;
                if (asValueChange != null)
                {
                    diff.NewValue = asValueChange.Value;
                }
                retVal.Add(diff);
            }
            return(retVal);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Reports the changes.
        /// </summary>
        /// <param name="initialGraph">The initial graph.</param>
        /// <param name="finalGraph">The final graph.</param>
        /// <returns>ChangeList.</returns>
        public static ChangeList ReportChanges(CaptureResults initialGraph, CaptureResults finalGraph)
        {
            var instance = new GraphCompare();

            return(instance.Execute(initialGraph, finalGraph));
        }