This project has moved and is read-only. For the latest updates, please go here.

using "Base Class" properties?

Feb 10, 2010 at 9:06 PM

Is there any way to use inheritance to utilize varying objects of a compatible base type in a collection?

For example, I have a configuration collection "Data Sources". In it, I could have an "FTP Data Source", "UNC Data Source", "HTTP Data Source", all inheriting from a base class "Data Source".

It seems natural, but currently the designer won't let me do it... It only allows collection elements of type "Data Source", not any sub-classing elements.

Am I going about it the wrong way?

I know there's a dirty workaround using "custom child elements", but that is very dirty. If I use that, the subclassing elements do not have any intellisence in app.config, they are in essence not considered to be part of the schema when the schema is built.

Feb 11, 2010 at 8:28 AM

What do you mean it only allows elements of type "Data Source"? What is the warning/error/exception you get, and where/when?

Feb 11, 2010 at 3:14 PM

Here's what I have:

I have a Configuration element collection  DataSourceCollection (<dataSources>) with item type "DataSourceDefinition".

Then I have a basic configuration element called "DataSourceDefinition".

Then I have a couple of other more advanced configuration elements called FTPDataSourceDefinition and UNCDataSourceDefinition, both marked with the "Base Class" propery as "DataSourceDefinition". There's a arrow link that shows now from those config elements to DataSourceDefinition configuration element.

What happens when I try to substitute <dataSourceDefinition /> elements in the collection in web.config by <ftpDataSourceDefinition /> or <uncDataSourceDefinition />, app.config complains as follows:

Warning    9    The element 'dataSources' in namespace 'DataSourceConfig' has invalid child element 'ftpDataSourceDefinition' in namespace 'DataSourceConfig'. List of possible elements expected: 'dataSourceDefinition' in namespace 'DataSourceConfig'.   


Feb 11, 2010 at 9:03 PM

Hm, well, it's just an XML Schema warning. Have you tried running it anyway to see if it actually works? When I did my testing with inheritance in collections, the results were rather disappointing. I could never get the configuration system to figure out which class type to instantiate.

Feb 11, 2010 at 9:23 PM

What told me that it's not working is that there is no intellisense on the subclassing types in app.config. There is intellisense on dataSourceDefinition, but never on ftpDataSourceDefinition or uncDataSourceDefinition. It's as if they do not exist as far as the schema is concerned.


Feb 11, 2010 at 10:32 PM

The reason they are not showing up in the intellisense is because CSD has not been coded to include them, because I believe that the configuration system does not support this. However, if you're interested, try anyway and see what happens.