Ejemplo n.º 1
0
        public static T GetAppSetting <T>(string key, T defaultValue = default(T))
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key value cannot be null or empty");
            }

            string value = null;

            try
            {
                value = ConfigurationManager.AppSettings[key];
            }
            catch (ConfigurationErrorsException)
            {
                // Ignore
            }

            if (value != null)
            {
                var type = typeof(T);
                try
                {
                    if (type.IsEnum)
                    {
                        return((T)Enum.Parse(type, value.ToString(), true));
                    }

                    return((T)Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
                }
                catch (Exception ex)
                {
                    var msg = StringExt.Format("Failed to convert AppSetting '{0}={1}' to type '{2}'. Using default value '{3}'", key, value, type, defaultValue);
                    throw new ConfigurationErrorsException(msg, ex);
                }
            }

            return(defaultValue);
        }
Ejemplo n.º 2
0
 public void Warning(string message, params object[] args)
 {
     WriteLine(LogTime() + " (warning):" + StringExt.Format(message, args), ConsoleColor.Yellow);
 }
Ejemplo n.º 3
0
 public void Info(string message, params object[] args)
 {
     WriteLine(LogTime() + " (info):" + StringExt.Format(message, args), ConsoleColor.Green);
 }
Ejemplo n.º 4
0
 public void Error(string message, params object[] args)
 {
     WriteLine(LogTime() + " (error):" + StringExt.Format(message, args), ConsoleColor.DarkRed);
 }
Ejemplo n.º 5
0
 public void Debug(string message, params object[] args)
 {
     WriteLine(LogTime() + " (debug):" + StringExt.Format(message, args), ConsoleColor.Gray);
 }
Ejemplo n.º 6
0
 public static MapperMissingPropertyException From(PropertyInfo propertyInfo)
 {
     return
         (new MapperMissingPropertyException(
              StringExt.Format("Missing property: {0} in the destination object", propertyInfo.Name)));
 }
Ejemplo n.º 7
0
 public void Format_HappyPath()
 {
     StringExt.Format("{Number} {Date:yyyyMM}-{Number,4:x}", new { Number = 234, Date = new DateTime(2021, 02, 14) })
     .Should().Be("234 202102-  ea");
 }
Ejemplo n.º 8
0
            private void RefreshUi()
            {
                lock (_rects)
                {
                    _rects.Clear();
                    var grid = new Grid();
                    grid.CreateRows("50*", "50*");

                    grid.Cell().Row(1)
                    .AddUi(
                        new Label()
                    {
                        Content             = _uiCard.CenterFrequency,
                        FontWeight          = FontWeights.Black,
                        HorizontalAlignment = HorizontalAlignment.Center,
                        Foreground          = Application.Current.Resources["SpectrumCardTextBrush"] as Brush
                    })
                    .AddUi(
                        new Label()
                    {
                        Content             = _uiCard.Name,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Foreground          = Application.Current.Resources["SpectrumCardNameBrush"] as Brush
                    })
                    .AddUi(
                        new Label()
                    {
                        //This is a shortcut to display the stream direction without picture using a webding character
                        FontFamily          = new FontFamily("Webdings"),
                        FontSize            = 16,
                        Content             = (_uiCard.StreamDirection == StreamDirectionEnum.Down) ? "6" : "5",
                        HorizontalAlignment = HorizontalAlignment.Right,
                        Foreground          = Application.Current.Resources["SpectrumCardNameBrush"] as Brush
                    }
                        );

                    grid.Cell().Row(0).AddUi(
                        new CustomCanvas(
                            canvas =>
                    {
                        int i = 0;
                        Trace.WriteLine(StringExt.Format("Create: {0} - {1}", _uiCard.Name, _uiCard.Channels.Count));

                        foreach (var channel in _uiCard.Channels)
                        {
                            var rect = new Rectangle()
                            {
                                Fill   = ChannelColors.GetChannelColor(channel),
                                Margin = new Thickness(0, 3, 0, 3),
                                Stroke = Application.Current.Resources["SpectrumCardChannelStateBorderBrush"] as Brush
                            };
                            _rects.Add(rect);
                            canvas.Children.Add(rect);
                            i += 1;
                        }
                        return(true);
                    },
                            (c) =>
                    {
                        int i = 0;
                        foreach (var channel in _uiCard.Channels)
                        {
                            Trace.WriteLine(
                                StringExt.Format("Positioning : {0} - {1}", _uiCard.Name, _uiCard.Channels.Count));

                            var px = c.ActualWidth / 200
                                     * (channel.CenterFreq - (_uiCard.CenterFrequency - 100)
                                        - (channel.Bandwidth / 2));
                            var size = c.ActualWidth / 200 * channel.Bandwidth;

                            (c.Children[i] as Rectangle).Width  = size;
                            (c.Children[i] as Rectangle).Height = c.ActualHeight;
                            Canvas.SetLeft(c.Children[i], px);
                            Canvas.SetTop(c.Children[i], 0);
                            i += 1;
                            Trace.WriteLine(StringExt.Format("Positioning : {0} - {1}", _uiCard.Name, px));
                        }
                    }));
                    Trace.WriteLine(StringExt.Format("{0} - {1}", _uiCard.Name, _uiCard.Channels.Count));
                    Child = grid;
                    if (ActualHeight < 50)
                    {
                        grid.RowDefinitions[0].Height = new GridLength(0);
                    }

                    InvalidateVisual();
                }
            }
Ejemplo n.º 9
0
        public void Format_ValueTupleData_ThrowsArgumentException()
        {
            Action action = () => StringExt.Format("{Number}", (Number: 7, Text: "hello"));

            action.Should().Throw <ArgumentException>();
        }
Ejemplo n.º 10
0
        public void Format_NullObjectIfNotNeeded_Success()
        {
            Action action = () => StringExt.Format("No placeholders", null);

            action.Should().NotThrow();
        }
Ejemplo n.º 11
0
        public void Format_NullObject_ThrowsArgumentNullException()
        {
            Action action = () => StringExt.Format("{Number}", null);

            action.Should().Throw <ArgumentNullException>();
        }
Ejemplo n.º 12
0
        public void Format_NullFormatString_ThrowsArgumentException()
        {
            Action action = () => StringExt.Format(null, new { Number = 234, Date = new DateTime(2021, 02, 14) });

            action.Should().Throw <ArgumentException>();
        }
Ejemplo n.º 13
0
        public void Format_NoItemNameAndThenComma_ThrowsFormatException()
        {
            Action action = () => StringExt.Format("{Number} {,7}-{}", new { Number = 234, Date = new DateTime(2021, 02, 14) });

            action.Should().Throw <FormatException>();
        }
Ejemplo n.º 14
0
        public void Format_EmptyBraces_ThrowsFormatException()
        {
            Action action = () => StringExt.Format("{Number} {Date:yyyyMM}-{}", new { Number = 234, Date = new DateTime(2021, 02, 14) });

            action.Should().Throw <FormatException>();
        }
Ejemplo n.º 15
0
        private void Disect()
        {
            stackVals.Children.Clear();
            stackKeys.Children.Clear();

            List <UiInfo> infos = new List <UiInfo>();

            ItemToShow.GetType().GetProperties().ForEach(x =>
            {
                try
                {
                    var ui = x.GetCustomAttribute(typeof(UiInformationAttribute), true);
                    if (ui != null)
                    {
                        var item       = ui as UiInformationAttribute;
                        bool isVisible = true;
                        if (!string.IsNullOrEmpty(item.IsVisible))
                        {
                            // must look for function
                            var method = ItemToShow.GetType().GetMethod(item.IsVisible, BindingFlags.NonPublic | BindingFlags.Instance);
                            if (method == null)
                            {
                                var message =
                                    StringExt.Format("Missing method: {0} for UiInformationAttribute in class {1}", item.IsVisible,
                                                     ItemToShow.GetType().Name);
                                _log.Error(message);
                                throw new UiInformationException(message);
                            }
                            if (method.ReturnType != typeof(bool))
                            {
                                var message =
                                    StringExt.Format("UiInformationAttribute.IsVisible: {0} method must return a bool", item.IsVisible);
                                _log.Error(message);
                                throw new UiInformationException(message);
                            }
                            isVisible = (bool)method.Invoke(ItemToShow, null);
                        }

                        if (isVisible)
                        {
                            infos.Add(new UiInfo()
                            {
                                Attribute = item, Prop = x
                            });
                        }
                    }
                }
                catch (Exception pokemon)
                {
                    _log.Error(pokemon.Message, pokemon);
                    throw;
                }
            });

            infos.Sort(
                (info, uiInfo) =>
            {
                if (info.Attribute.Position < uiInfo.Attribute.Position)
                {
                    return(-1);
                }
                if (info.Attribute.Position > uiInfo.Attribute.Position)
                {
                    return(1);
                }
                return(0);
            });

            foreach (var uiInfo in infos)
            {
                var labelKey = new StackPanel()
                {
                    Height = ItemSize, HorizontalAlignment = HorizontalAlignment.Left
                };

                string name = uiInfo.Attribute.Name;
                if (name == null)
                {
                    var nameProperty = ItemToShow.GetType().GetProperty(uiInfo.Attribute.NameProperty, BindingFlags.Instance | BindingFlags.NonPublic);
                    name = (string)nameProperty.GetMethod.Invoke(ItemToShow, null);
                }

                labelKey.Children.Add(new Label()
                {
                    Content = name,
                    Style   = (FindResource("LabelNamePanelList") as Style)
                });
                stackKeys.Children.Add(labelKey);
                var binding = new Binding(uiInfo.Prop.Name);
                binding.Source = ItemToShow;

                if (uiInfo.Prop.PropertyType.IsEnum)
                {
                    binding.Converter = new EnumToStringConverter();
                }

                var child = new Label()
                {
                    Style = (FindResource("LabelValuePanelList") as Style), HorizontalAlignment = HorizontalAlignment.Left
                };
                var labelVal = new StackPanel()
                {
                    Height = ItemSize, HorizontalAlignment = HorizontalAlignment.Left
                };
                labelVal.Children.Add(child);
                child.SetBinding(Label.ContentProperty, binding);
                stackVals.Children.Add(labelVal);
                if (uiInfo.Attribute.Editor != null)
                {
                    var control = Activator.CreateInstance(uiInfo.Attribute.Editor) as Control;

                    if (control != null)
                    {
                        control.HorizontalAlignment = HorizontalAlignment.Left;
                        control.VerticalAlignment   = VerticalAlignment.Center;
                        control.FontSize            = 20;
                        control.Height = ItemSize;
                    }

                    if (control is ComboBox && uiInfo.Prop.PropertyType.IsEnum)
                    {
                        var converter = new EnumToStringConverter();
                        foreach (var val in uiInfo.Prop.PropertyType.GetEnumValues())
                        {
                            var convertedVal = converter.Convert(val, null, null, null);
                            (control as ComboBox).Items.Add(convertedVal);
                        }
                    }
                    Editor editor = child.BindEditor().Set(control);
                    editor.OnEditorShow += EditorOnOnEditorShow;
                    _editors.Add(editor);
                }
            }
        }
Ejemplo n.º 16
0
 public override string ToString()
 {
     return(Value != string.Empty ? Value : StringExt.Format("{0}", Index));
 }
Ejemplo n.º 17
0
        public void Format_PropertyNotFound_ThrowsArgumentException()
        {
            Action action = () => StringExt.Format("{Other}", new { Number = 7, Text = "hello" });

            action.Should().Throw <ArgumentException>();
        }