Example #1
0
        public static T GenerateNew( )
        {
            T setting = new T( );

            foreach (PropertyInfo property in typeof(T).GetProperties( ))
            {
                SettingItemAttribute attribute =
                    ( SettingItemAttribute )property.GetCustomAttribute(typeof(SettingItemAttribute));

                if (!(attribute is null))
                {
                    property.SetValue(setting, attribute.DefaultValue);
                }
            }

            return(setting);
        }
Example #2
0
        public string Save( )
        {
            StringBuilder [] stringBuilders =
                new StringBuilder[Enum.GetValues(typeof(TSettingCategory)).Length];

            foreach (TSettingCategory type in Enum.GetValues(typeof(TSettingCategory)))
            {
                stringBuilders [type.ToInt32(CultureInfo.InvariantCulture)] = new StringBuilder( );
            }

            foreach (PropertyInfo property in typeof(T).GetProperties( ))
            {
                SettingItemAttribute attribute =
                    ( SettingItemAttribute )property.GetCustomAttribute(typeof(SettingItemAttribute));

                if (!(attribute is null))
                {
                    int           index           = attribute.SettingCategory;
                    StringBuilder propertyBuilder = stringBuilders [index];
                    propertyBuilder.AppendLine(attribute.ToString( ));
                    propertyBuilder.AppendLine($"{property . Name} = {property . GetValue ( this )}");
                    propertyBuilder.AppendLine( );
                }
            }

            StringBuilder builder = new StringBuilder( );

            for (int i = 0; i < stringBuilders.Length; i++)
            {
                builder.AppendLine(
                    $"##{( TSettingCategory ) Enum . ToObject ( typeof ( TSettingCategory ) , i )}");
                builder.AppendLine( );
                builder.AppendLine(stringBuilders [i].ToString( ));
                builder.AppendLine( );
            }

            return(builder.ToString( ));
        }