Reputation: 65
I'm trying to implement a custom configuration class that would inherit System.Configuration.ConfigurationSection
as its base class.
public class MyConfigurationClass: System.Configuration.ConfigurationSection
{
MyConfigurationClass()
{
}
}
I've been creating the class object like this:
MyConfigurationClass Config = System.Configuration.ConfigurationManager.GetSection("Config") as MyConfigurationClass;
I want to move this into the constructor of my class so that instead of calling:
MyConfigurationClass Config = System.Configuration.ConfigurationManager.GetSection("Config") as MyConfigurationClass;
I could call:
MyConfigurationClass Config = new MyConfigurationClass("Config");
For the same ressult.
Inheriting elements of System.Configuration.ConfigurationSection
is critical.
Any suggestions?
Edit: I am trying to initialize the default instance of this class using unity mapping and then use interfaces for the subsequent calls to this class. My interface is fixed and can not be modified and thus I can only use constructor to create the class...
Registration into unity framework is done from unity.config
file:
<register type="MyNameSpace.IConfig, MyProject" mapTo="MyRealNameSpace.MyConfigurationClass, MyRealProject">
<lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
</register>
Upvotes: 0
Views: 463
Reputation: 29846
Personally, I like to add a static getter that fetches the configuration inside my section:
public class MyConfigurationClass : ConfigurationSection
{
public static MyConfigurationClass Instance { get; } =
ConfigurationManager.GetSection("Config") as MyConfigurationClass;
// other configuration properties
}
And then I simply call MyConfigurationClass.Instance.Property
Upvotes: 1