I'm working on a custom configuration section for a custom application, attempting to make the functionality more easily extensible. I've got an extension mechanism finished up for one part, to create a specific configuration type based on the element name, but for another part I need to be able to do the same based on an attribute value, and I can't figure out how to access that before the ConfigurationElement object gets created and initialized from those values.
Basically, I have a set of FileFormat* objects which inherit from ConfigurationElement object, and I need to use the value of the 'type' attribute to instantiate the correct specific object (*DelimitedText, *FixedText, *Excel) so their type-specific properties can be initialized.
Here's the format of the ConfigurationElement's XML.
<format headerRows="1" footerRows="0" type="delimited" delimiter="|">
<fields>
<field name="fieldName" maxLength="50" required="true" />
...
</fields>
</format >
Here's the code I'm trying to use to initialize it:
private Lib.Core.Config.FileFormatBase _sourceFormat = null;
[System.Configuration.ConfigurationProperty("sourceFormat", IsRequired = true)]
public Lib.Core.Config.FileFormatBase SourceFormat
{
get
{
if(null == this._sourceFormat)
{
System.String xml = this.SectionInformation.GetRawXml();
System.Configuration.PropertyInformation test = (System.Configuration.PropertyInformation)this.ElementInformation.Properties["sourceFormat"];
System.Configuration.ConfigurationElement element = (System.Configuration.ConfigurationElement)base["sourceFormat"];
System.String formatName = "";
this._sourceFormat = (Lib.Core.Config.FileFormatBase)Lib.Core.Config.FileFormatManager.Create(formatName, element);
}
return (this._sourceFormat);
}
private set
{
base["sourceFormat"] = value;
this._sourceFormat = null;
}
}
public static Lib.Core.Config.FileFormatBase Create(System.String formatName, System.Configuration.ConfigurationElement element)
{
System.Type formatType = GetRegisteredFormat(formatName);
Lib.Core.Config.FileFormatBase handler = (Lib.Core.Config.FileFormatBase)System.Activator.CreateInstance(formatType, element);
return (handler);
}