public static T Load <T>(string filePath) where T : YamlStaticConfig
 {
     try
     {
         if (File.Exists(filePath))
         {
             using (StreamReader r = new StreamReader(filePath, false))
             {
                 Deserializer s = new DeserializerBuilder()
                                  .Build();
                 YamlStaticConfig conv = s.Deserialize <T>(r);
                 conv.filePath = filePath;
                 return((T)Convert.ChangeType(conv, typeof(T)));
             }
         }
         else
         {
             object           ins  = Activator.CreateInstance(typeof(T));
             YamlStaticConfig conv = (YamlStaticConfig)ins;
             conv.filePath = filePath;
             conv.Save <T>();
             return((T)conv);
         }
     }
     catch
     {
         Logger.Error("%server_exception_config");
         Logger.Notice("%server_exception_config2", filePath);
         throw new IOException(LangManager.GetString("server_exception_config")
                               + Environment.NewLine
                               + string.Format(LangManager.GetString("server_exception_config2"), filePath));
     }
 }
Пример #2
0
 public static T Load <T>(string filePath) where T : YamlStaticConfig
 {
     try
     {
         if (File.Exists(filePath))
         {
             using (StreamReader r = new StreamReader(filePath, false))
             {
                 Deserializer s = (Deserializer) new DeserializerBuilder()
                                  .Build();
                 YamlStaticConfig conv = s.Deserialize <T>(r);
                 conv.filePath = filePath;
                 return((T)Convert.ChangeType(conv, typeof(T)));
             }
         }
         else
         {
             object           ins  = Activator.CreateInstance(typeof(T));
             YamlStaticConfig conv = (YamlStaticConfig)ins;
             conv.filePath = filePath;
             conv.Save <T>();
             return((T)conv);
         }
     }
     catch (SerializationException)
     {
         File.Move(filePath, filePath + ".old");
         return(YamlStaticConfig.Load <T>(filePath));
     }
     catch (YamlException)
     {
         File.Move(filePath, filePath + ".old");
         return(YamlStaticConfig.Load <T>(filePath));
     }
     catch (Exception e3)
     {
         throw e3;
     }
 }