Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Generic Class inheritance
Message
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 5.0
OS:
Windows 7
Network:
SAMBA Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01611057
Message ID:
01611058
Views:
54
>Hi All, I'm looking at using custom sections in app.config files and came across this code on the web
>
>
>public abstract class BaseConfigurationSection<T> : ConfigurationSection
>             where T : ConfigurationSection, new()
>    {
>        public static T Open()
>        {
>            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
>            if (config.Sections[typeof(T).Name] == null)
>            {
>                T instance = new T();
>                config.Sections.Add(typeof(T).Name, instance);
>                config.Save(ConfigurationSaveMode.Full);
>            }
>            return config.Sections[typeof(T).Name] as T;
>        }
>
>        public void Save()
>        {
>            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
>            T section = (T)configuration.Sections[typeof(T).Name];
>            CopyProperties(this, section);
>            configuration.Save(ConfigurationSaveMode.Full);
>        }
>
>        private static void CopyProperties(ConfigurationElement source, ConfigurationElement destination)
>        {
>            foreach (PropertyInfo property in source.GetType().GetProperties())
>            {
>                if (property.IsDefined(typeof(ConfigurationPropertyAttribute), false))
>                {
>                    if (typeof(ConfigurationElement).IsAssignableFrom(property.PropertyType))
>                    {
>                        CopyProperties(
>                            (ConfigurationElement)property.GetValue(source, null),
>                            (ConfigurationElement)property.GetValue(destination, null));
>                    }
>                    else
>                    {
>                        object value = property.GetValue(source, null);
>                        property.SetValue(destination, value, null);
>                    }
>                }
>            }
>        }
>
>    }
>
>
>Using this a base class you can then create your own custom sections as shown below
>
>
>
>public class MySection : BaseConfigurationSection<MySection>
>    {
>        public MySection()
>        {
>
>        }
>
>        [ConfigurationProperty("Destination", DefaultValue = "Some place", IsRequired = true)]
>        public string Destination
>        {
>            get 
>            {
>                return (string)this["Destination"];
>            }
>            set
>            {
>                this["Destination"] = value;
>            }
>        }
>
>        [ConfigurationProperty("DialCode", DefaultValue = "01932", IsRequired = true)]
>        public string DialCode
>        {
>            get
>            {
>                return (string)this["DialCode"];
>            }
>            set
>            {
>                this["DialCode"] = value;
>            }
>        }
>}
>
>
>All this seems to work fine, but I want to inherit from "MySection" and am unsure as to how to do it - I've tried the normal way of subclassing
>
>class MySection2:MySection
>{
>}
>
>
>but get errors about cannot implicitly convert MySection to MySection2 and so on - is what am trying possible ?

Your concrete MySection class is 'bound' to a MySection element in config.Sections so I'm not sure what you wish to achieve by sub-classing?

Also, the code seems a bit odd. Seems to me that calling Save() without some prior call to MySection.Open() will throw an exception since the section would not exist....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform