How can I make a custom configuration section that can contain attributes not defined at compile time?

StackOverflow https://stackoverflow.com/questions/13421065

Question

Please note this is not the actual case, but only a simplified example.

Lets say I have a Log class, then Sub classed from that I have a file log and a email log. Now lets say that I want to have a configuration section such as

   Logs>
     Log Type="file" fileName="log.txt" Verbosity="3"/>
     Log Type="email" emailAddress="email@server.com" Verbosity="1"/>
   /Logs>

Currently I have a solution that will allow

   Logs>
     Log Type="file" Verbosity="3"/>
     Log Type="email" Verbosity="1"/>
   /Logs>

But as soon as I attempt to add fileName or emailAddress* (this is only an example) I get an error because the configuration section for a base log class does not contain configurationProperties specific to the sub log types. Is there any way that this can be acomplished?

Était-ce utile?

La solution

You can override the OnDeserializeUnrecognizedAttribute method and store the value in some kind of property bag. If you look at the System.Diagnostics.ListenerElement class in the System assembly you can see it does just that; you'll need to get the framework source or use a decompiler for that.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top