/// <summary>
        /// Reads all properties from the given profile.
        /// Object's type name ti used as section name.
        /// </summary>
        /// <param name="obj">Object whos properties are to be read</param>
        /// <param name="profile">Profile containing data</param>
        public static void Load(object obj, Profile profile)
        {
            var type       = obj.GetType();
            var iniSection = profile[type.Name];
            var properties = type.GetProperties();

            foreach (var property in properties)
            {
                var value = iniSection.Get <object>(property.Name);
                if (value == null)
                {
                    continue;
                }

                try
                {
                    value = UniversalConverter.ConvertToType(value, property.PropertyType);
                    property.SetValue(obj, value, null);
                }
                catch
                {
                    // ignore
                }
            }
        }
        public void UniversalConverterShouldConvertElementTypes()
        {
            var intList = new List <int> {
                1, 2, 3, 4, 5
            };
            var stringList = intList.Select(e => e.ToString()).ToList();

            Assert.Equal(stringList, UniversalConverter.ConvertToType(intList, typeof(List <string>)));
        }
        public void UniversalConverterShouldConvertArraysToLists()
        {
            var list = new List <int> {
                1, 2, 3, 4, 5
            };
            var array = list.ToArray();

            Assert.Equal(list, UniversalConverter.ConvertToType(array, typeof(List <int>)));
        }
        public void UniversalConverterShouldConvertStringsToBoolean()
        {
            Assert.False((bool)UniversalConverter.ConvertToType("0", typeof(bool)));
            Assert.False((bool)UniversalConverter.ConvertToType("N", typeof(bool)));
            Assert.False((bool)UniversalConverter.ConvertToType("F", typeof(bool)));
            Assert.False((bool)UniversalConverter.ConvertToType("false", typeof(bool)));
            Assert.False((bool)UniversalConverter.ConvertToType("False", typeof(bool)));
            Assert.False((bool)UniversalConverter.ConvertToType("X", typeof(bool)));
            Assert.False((bool)UniversalConverter.ConvertToType("", typeof(bool)));

            Assert.True((bool)UniversalConverter.ConvertToType("1", typeof(bool)));
            Assert.True((bool)UniversalConverter.ConvertToType("true", typeof(bool)));
            Assert.True((bool)UniversalConverter.ConvertToType("True", typeof(bool)));
            Assert.True((bool)UniversalConverter.ConvertToType("Y", typeof(bool)));
            Assert.True((bool)UniversalConverter.ConvertToType("J", typeof(bool)));
            Assert.True((bool)UniversalConverter.ConvertToType("T", typeof(bool)));
        }
Пример #5
0
        void TreeViewExSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e)
        {
            var newValue = e.NewValue;

            if (!string.IsNullOrEmpty(SelectedItemType))
            {
                var type =
                    AppDomain.CurrentDomain.GetAssemblies()
                    .Where(a => a.GetType(SelectedItemType) != null)
                    .Select(a => a.GetType(SelectedItemType))
                    .FirstOrDefault();
                if (type != null)
                {
                    newValue = UniversalConverter.ConvertToType(newValue, type);
                }
            }
            SelectedItem = newValue;
        }
Пример #6
0
        public T GetObject <T>(CultureInfo cultureInfo) where T : new()
        {
            var obj = new T();

            foreach (var propertyInfo in typeof(T).GetProperties())
            {
                var columnIndex = ColumnNames
                                  .FindIndex(c => string.Compare(c, propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase) == 0);
                if (columnIndex == -1)
                {
                    continue;
                }

                var value = UniversalConverter.ConvertToType(GetField(Columns[columnIndex]), propertyInfo.PropertyType, cultureInfo);
                propertyInfo.SetValue(obj, value, null);
            }

            return(obj);
        }
        public void SetProperties(IPropertyProvider propertyValues, IFormatProvider provider)
        {
            var properties = _type.GetProperties();

            foreach (var property in properties)
            {
                var value = propertyValues.Get <object>(property.Name);
                if (value != null)
                {
                    // for array and list properties - split value on ';'s
                    if (property.PropertyType.IsArray ||
                        (property.PropertyType.IsGenericType &&
                         property.PropertyType.GenericTypeArguments.Length == 1 &&
                         property.PropertyType == typeof(List <>).MakeGenericType(property.PropertyType.GenericTypeArguments)))
                    {
                        value = value.ToString().Split(';').ToList();
                    }
                    value = UniversalConverter.ConvertToType(value, property.PropertyType, provider);
                    property.SetValue(_targetObject, value, null);
                    Trace.TraceInformation($"Set({_type.Name}) {property.Name} = {property.GetValue(_targetObject, null)}");
                }
            }
        }
Пример #8
0
        public static CsvData FromObject(object obj, CultureInfo cultureInfo)
        {
            var columns = obj.GetType().GetProperties()
                          .Select(prop => prop.Name)
                          .ToList();

            var csvData = new CsvData(-1, columns, "");

            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                var columnIndex = csvData.ColumnNames
                                  .FindIndex(c => string.Compare(c, propertyInfo.Name, StringComparison.InvariantCultureIgnoreCase) == 0);
                if (columnIndex == -1)
                {
                    continue;
                }

                var value = UniversalConverter.ConvertToType(propertyInfo.GetValue(obj), typeof(string), cultureInfo)?.ToString() ?? "";
                csvData.Fields.Add(value);
            }

            return(csvData);
        }
 public void UniversalConverterShouldConvertIntegersToEnums()
 {
     Assert.Equal(TestEnum.Three, UniversalConverter.ConvertToType(3, typeof(TestEnum)));
 }
 public void UniversalConverterShouldConvertIntegersToBoolean()
 {
     Assert.False((bool)UniversalConverter.ConvertToType(0, typeof(bool)));
     Assert.True((bool)UniversalConverter.ConvertToType(1, typeof(bool)));
 }