I am following this great article on how to create a Provider framework in .NET
Basically, this article explains greatly how to end up with a configuration file like the following:
<configuration>
<configSections>
<section name="data" type="DataProviderConfigurationSection" />
</configSections>
<data defaultProvider="MyDataProvider">
<providers>
<add name="MydataProvider" type="MyDataProvider" />
</providers>
</data>
</configuration>
Where the <add/>
element allows you to define a provider.
However, I would like to know how to extend the add
entry with custom attributes.
For example:
<providers>
<add name="MydataProvider" type="MyDataProvider" myProperty="myValue" myProperty2="myValue2" ... />
</providers>
Any help will be greatly appreciated.
Here is what I finally found. This is a very specific question about extending the element with more attributes and how to handle them when implementing a Provider Framework. All the answers about custom configuration sections are OK but not addressing the original question.
If you need to implement a custom Provider, like the
MembershipProvider
, but for your own purpose, you need to definitely read this article: Creating Your Own Provider FrameworkIt is excellent reading. Now if you need to extend the element with your own attributes, here is what you need to change...
1) Next is the code discussed in the article (There might be some adaptations):
2) This is the config file that you use for the above code:
3) Now, here is what you need to modify in order to use read the attributes in the
<add>
element in the configuration file.4) The configuration file looks like this:
And as a bonus, here is a Unit test to validate it works:
Like:
?
The c# codes:
You can use ConfigurationProperty and ProviderBase
You need to create a custom ConfigurationSection implementation, which will allow you to create a customized config tree like you provided.