internal static void SetPropertyValue(System.Reflection.PropertyInfo property, object obj, string value)
        {
            value = value.Trim(new char[] { '"' });
            Type   t   = property.PropertyType;
            object val = null;

            if (t.BaseType == typeof(Enum))
            {
                val = EnumUtility.GetValueByName(t, value);
                property.SetValue(obj, val, null);
            }

            if (t == typeof(Int16))
            {
                val = ConvertUtility.ToInt16(value, 0);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(Int32))
            {
                val = ConvertUtility.ToInt32(value, 0);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(Int64))
            {
                val = ConvertUtility.ToInt64(value, 0);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(byte))
            {
                val = ConvertUtility.ToByte(value, 0);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(double))
            {
                val = ConvertUtility.ToDouble(value, 0);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(decimal))
            {
                val = ConvertUtility.ToDecimal(value, 0);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(bool))
            {
                val = ConvertUtility.ToBoolean(value, false);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(Int16?))
            {
                val = ConvertUtility.ToInt16Nullable(value);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(Int32?))
            {
                val = ConvertUtility.ToInt32Nullable(value);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(Int64?))
            {
                val = ConvertUtility.ToInt64Nullable(value);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(byte?))
            {
                val = ConvertUtility.ToByteNullable(value);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(double?))
            {
                val = ConvertUtility.ToDoubleNullable(value);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(decimal?))
            {
                val = ConvertUtility.ToDecimalNullable(value);
                property.SetValue(obj, val, null);
            }
            if (t == typeof(string))
            {
                property.SetValue(obj, value, null);
            }

            if (t == typeof(char))
            {
                char[] cval = value.ToCharArray();
                if (cval.Length > 0)
                {
                    property.SetValue(obj, cval[0], null);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// replaces #[Special folder name]
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ParseSpecialFoldersNames(string path, ParseSpecialFolderOption option)
        {
            if (path == null)
            {
                return(string.Empty);
            }
            string[] names = Enum.GetNames(typeof(Environment.SpecialFolder));
            if (option == ParseSpecialFolderOption.WildCardToRealPath)
            {
                foreach (string name in names)
                {
                    Environment.SpecialFolder specialfolder = (Environment.SpecialFolder)EnumUtility.GetValueByName(typeof(Environment.SpecialFolder), name);
                    path = path.Replace(string.Format("#{0}", name), Environment.GetFolderPath(specialfolder));
                }
                return(path);
            }

            foreach (string name in names)
            {
                Environment.SpecialFolder specialfolder = (Environment.SpecialFolder)EnumUtility.GetValueByName(typeof(Environment.SpecialFolder), name);
                string toreplace = Environment.GetFolderPath(specialfolder);
                if (!string.IsNullOrEmpty(toreplace))
                {
                    path = path.Replace(Environment.GetFolderPath(specialfolder), string.Format("#{0}", name));
                }
            }
            return(path);
        }