コード例 #1
0
ファイル: ObjectNode.cs プロジェクト: oip976/TrafficMirror
        private void ParseObjectTree(string name, object value, Type type)
        {
            Children = new List <ObjectNode>();

            _type = type;
            _name = name;

            if (value != null)
            {
                if (value is string && type != typeof(object))
                {
                    if (value != null)
                    {
                        _value = "\"" + value + "\"";
                    }
                }
                else if (value is double || value is bool || value is int || value is float || value is long || value is decimal)
                {
                    _value = value;
                }
                else if (value is DateTime)
                {
                    try
                    {
                        _value = ((DateTime)value).ToString("o");
                    }
                    catch { }
                }
                else
                {
                    _value = "{" + value.ToString() + "}";
                }
            }

            PropertyInfo[] props = type.GetProperties();

            if (props.Length == 0 && type.IsClass && value is IEnumerable && !(value is string))
            {
                IEnumerable arr = value as IEnumerable;

                if (arr != null)
                {
                    int i = 0;
                    foreach (object element in arr)
                    {
                        Children.Add(new ObjectNode("[" + i + "]", element, element.GetType()));
                        i++;
                    }
                }
            }

            foreach (PropertyInfo p in props)
            {
                if (p.PropertyType.IsPublic)
                {
                    if (p.PropertyType.IsClass || p.PropertyType.IsArray || p.PropertyType.FullName.StartsWith("System.Collections.Generic.List"))
                    {
                        if (p.PropertyType.IsArray || p.PropertyType.FullName.StartsWith("System.Collections.Generic.List"))
                        {
                            try
                            {
                                object      v   = p.GetValue(value, null);
                                IEnumerable arr = v as IEnumerable;

                                ObjectNode arrayNode = new ObjectNode(p.Name, arr.ToString(), typeof(object));

                                if (arr != null)
                                {
                                    int        i = 0, k = 0;
                                    ObjectNode arrayNode2;

                                    foreach (object element in arr)
                                    {
                                        //Handle 2D arrays
                                        if (element is IEnumerable && !(element is string))
                                        {
                                            arrayNode2 = new ObjectNode("[" + i + "]", element.ToString(), typeof(object));

                                            IEnumerable arr2 = element as IEnumerable;
                                            k = 0;

                                            foreach (object e in arr2)
                                            {
                                                arrayNode2.Children.Add(new ObjectNode("[" + k + "]", e, e.GetType()));
                                                k++;
                                            }

                                            arrayNode.Children.Add(arrayNode2);
                                        }
                                        else
                                        {
                                            arrayNode.Children.Add(new ObjectNode("[" + i + "]", element, element.GetType()));
                                        }
                                        i++;
                                    }
                                }

                                Children.Add(arrayNode);
                            }
                            catch { }
                        }
                        else
                        {
                            object v = p.GetValue(value, null);

                            if (v != null)
                            {
                                Children.Add(new ObjectNode(p.Name, v, p.PropertyType));
                            }
                        }
                    }
                    else if (p.PropertyType.IsValueType && !(value is string) && !(value is DateTime))
                    {
                        try
                        {
                            object v = p.GetValue(value, null);

                            if (v != null)
                            {
                                Children.Add(new ObjectNode(p.Name, v, p.PropertyType));
                            }
                        }
                        catch { }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Method triggers the request by calling the execute method of the BingMaps RESTToolKit.
        /// <param name="request"></param>
        private async void ProcessRequest(BaseRestRequest request)
        {
            try
            {
                RequestProgressBar.Visibility = Visibility.Visible;
                RequestProgressBarText.Text   = string.Empty;

                ResultTreeView.ItemsSource = null;

                var start = DateTime.Now;

                //Executes the request.
                var response = await request.Execute((remainingTime) =>
                {
                    if (remainingTime > -1)
                    {
                        _time = TimeSpan.FromSeconds(remainingTime);

                        RequestProgressBarText.Text = string.Format("Time remaining {0} ", _time);

                        _timer.Start();
                    }
                });

                RequestUrlTbx.Text = request.GetRequestUrl();

                var end            = DateTime.Now;
                var processingTime = end - start;
                var nodes_warnings = new List <ObjectNode>();
                var nodes_all      = new List <ObjectNode>();
                //Takes any Object and generates an Object Tree.
                var tree = await ObjectNode.ParseAsync("result", response);

                //List of warnings for trim function and visualisation in the view
                var    list_warnings   = new List <KeyValuePair <string, string> >();
                string filter_severity = "Severity";

                ProcessingTimeTbx.Text = string.Format(CultureInfo.InvariantCulture, "{0:0} ms",
                                                       processingTime.TotalMilliseconds);
                RequestProgressBar.Visibility = Visibility.Collapsed;

                nodes_all.Add(tree);

                SeverityRatings_panel.Children.Clear();

                nodes_warnings.Add(tree);
                ResultTreeViewFull.ItemsSource = nodes_all;

                TrimTree(nodes_warnings, filter_severity, list_warnings);

                ResultTreeView.ItemsSource = nodes_warnings;

                foreach (var element in list_warnings)
                {
                    switch (element.Value)
                    {
                    case "\"Minor\"":
                    case "\"LowImpact\"":
                        TextBlock Minor_Low_sev = new TextBlock
                        {
                            Text       = element.ToString(),
                            Background = Brushes.LightGreen,
                            FontSize   = 20
                        };
                        SeverityRatings_panel.Children.Add(Minor_Low_sev);
                        break;

                    case "\"Moderate\"":
                        TextBlock Moderate_sev = new TextBlock
                        {
                            Text       = element.ToString(),
                            Background = Brushes.Orange,
                            FontSize   = 20
                        };
                        SeverityRatings_panel.Children.Add(Moderate_sev);
                        break;

                    case "\"Serious\"":
                        TextBlock Serious_sev = new TextBlock
                        {
                            Text       = element.ToString(),
                            Background = Brushes.PaleVioletRed,
                            FontSize   = 20
                        };
                        SeverityRatings_panel.Children.Add(Serious_sev);
                        break;

                    default:
                        break;
                    }
                }

                ProcessWarnings(list_warnings);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            _timer.Stop();
            RequestProgressBar.Visibility = Visibility.Collapsed;
        }