How to get specific attribute from Polymorphic collection

Topics: Building and extending application blocks, Enterprise Library Core, General discussion
Sep 14, 2007 at 4:57 AM
Hi,

I have created a custom configuration section for my application specific settings. Following is the snapshot of my custom configuration section:
*<FilesConfiguration>
<files>
<add name="Text File" friendlyName="Notepad Text File" index="1" fileDataType="MyApp.Files.TextFileData, FilesSets" type="" defaultText="This is a text file">
<add name="C# File" friendlyName="C# Code Text" index="2" fileDataType="MyApp.Files.CSharpFileData, FilesSets" type="" runTimeVersion="2.0.50727">
</FilesConfiguration>*

Following classes are there to read configuration section properly:

1. FilesConfigurationSettings inherited from ConfigurationSection (for FilesConfiguration tag)
2. BaseFileData inherited from NameTypeConfigurationElement
3. TextFileData and CSharpFileData both inherited from BaseFileData
4. FileDataCollection inherited from PolymorphicConfigurationElementCollection<BaseFileData> (for files tag)

More file types will be added in future and developer have to create a class for each newly added file type and should inherit it from BaseFileData. But each file may have specific attributes like defaultText in TextFileData and runTimeVersion in CSharpFileData.

The problem here is that how can I design BaseFileData to get specific attributes of each files without using Reflection? and also please tell me how ObjectBuilder helps me in this problem or any other ready component of EL.

Please refer any article, book or any type of material which will be helpful to me for this problem.

Thanks
Zeeshan Gulzar
Sep 14, 2007 at 10:00 AM
Edited Sep 14, 2007 at 12:47 PM
Your configuration should look like this instead (look at type attribute and remove fileDataType attrib)

<FilesConfiguration>
<files>
<add name="Text File" friendlyName="Notepad Text File" index="1" type="MyApp.Files.TextFileData, FilesSets" defaultText="This is a text file">
<add name="C# File" friendlyName="C# Code Text" index="2" type="MyApp.Files.CSharpFileData, FilesSets" runTimeVersion="2.0.50727">
</FilesConfiguration>

TextFileData and CSharpFileData should declare their respective properties as ConfigurationProperty

public class TextFileData : BaseFileData
{
private const string DefaultTextPropertyName = "defaultText";

[ConfigurationProperty(DefaultTextPropertyName)]
public string DefaultText
{
get { return (string)base[DefaultTextPropertyName]; }
set { base[DefaultTextPropertyName] = value; }
}
}

There are so many great examples of Polymorphic Config Types in the EntLib source itself, you should'nt have a hard time finding what ur looking for.
Sep 18, 2007 at 2:39 AM
Thank you for correcting me regarding type.

I have FileDataCollection object at run-time which is a PolymorphicConfigurationElementCollection<BaseFileData>. The properties like DefaultText and RuntimeVersion which is the properties of child classes of BaseFileData and I don't know how many child classes of BaseFileData are there so i can't cast the base object to any of it's child because of no knowledge at run-time.

One solution could be that I introduce a method in BaseFileData Class called object GetSpecificAttribute(string attributeName) and then use reflection to get the value of this attribute but I want not to use reflection and this solution always box the values to object in case of primitive.

Any solution?

Again Thanks for you reply.

Zeeshan Gulzar