Ejemplo n.º 1
0
        private static void ConfiSavePropertyAnalysis(Object target, PropertyInfo prop)
        {
            var configTag = TypeInfoProvider.GetConfigTag(prop);

            if (configTag != null)
            {
                Object value = TypeInfoProvider.GetValue(prop, target);
                if (configTag.IsEncrypted && value != null && prop.PropertyType == typeof(String))
                {
                    String strValue = value as String;
                    strValue = DefaultStringEncryptor.Encrypt(strValue);
                    TypeInfoProvider.SetValue(prop, target, strValue);
                    return;
                }
                if (prop.PropertyType.IsSubclassOf(typeof(ConfigBase)))
                {
                    Object valueCopy = TypeInfoProvider.CreateInstance(prop.PropertyType);
                    CopyConfigObject(value, valueCopy, prop.PropertyType);
                    TypeInfoProvider.SetValue(prop, target, valueCopy);
                    Type type          = prop.PropertyType;
                    var  propertyInfos = TypeInfoProvider.GetProperties(type);
                    foreach (var cprop in propertyInfos)
                    {
                        ConfiSavePropertyAnalysis(valueCopy, cprop);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从指定路径加载配置对象
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="isEncrypted">表示文件是否被加密</param>
        /// <returns></returns>
        public static Object LoadConfig(Type type, String filePath, Boolean isEncrypted = false)
        {
            Object target = TypeInfoProvider.CreateInstance(type);

            LoadConfigToObject(target, type, filePath, isEncrypted);
            return(target);
        }
Ejemplo n.º 3
0
        public static T LoadConfig <T>(String filePath, Boolean isEncrypted = false) where T : ConfigBase
        {
            Type   type   = typeof(T);
            Object target = TypeInfoProvider.CreateInstance(typeof(T));

            LoadConfigToObject(target, type, filePath, isEncrypted);
            return(target as T);
        }
Ejemplo n.º 4
0
        private static void CopyConfigObject(Object source, Object dest, Type type)
        {
            var propertyInfos = TypeInfoProvider.GetProperties(type);

            foreach (var prop in propertyInfos)
            {
                Object value = TypeInfoProvider.GetValue(prop, source);
                TypeInfoProvider.SetValue(prop, dest, value);
            }
        }
Ejemplo n.º 5
0
        public static void LoadConfigToObject(Object obj, Type type, String filePath, Boolean isEncrypted = false)
        {
            Object target = null;

            var text = File.ReadAllText(filePath, _DefaultEncodeing);

            if (isEncrypted)
            {
                text = DefaultStringEncryptor.Decrypt(text);
            }
            target = SerializeAssistor.Deserialize(text, type);

            var propertyInfos = TypeInfoProvider.GetProperties(type);

            foreach (var prop in propertyInfos)
            {
                ConfigLoadPropertyAnalysis(target, prop);
            }
            CopyConfigObject(target, obj, type);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 将配置对象保存到指定路径
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="savePath">保存路径  </param>
        /// <param name="isEncrypt">表示是否需要加密</param>
        public static void SaveConfig(Object obj, Type type, String savePath, Boolean isEncrypt = false)
        {
            Object target      = obj;
            Object cloneTarget = Activator.CreateInstance(type);

            CopyConfigObject(target, cloneTarget, type);

            var propertyInfos = TypeInfoProvider.GetProperties(type);

            foreach (var prop in propertyInfos)
            {
                ConfiSavePropertyAnalysis(cloneTarget, prop);
            }

            var text = SerializeAssistor.Serialize(cloneTarget, type);

            if (isEncrypt)
            {
                text = DefaultStringEncryptor.Decrypt(text);
            }
            File.WriteAllText(savePath, text, _DefaultEncodeing);
        }