How to access the ConfigurationPropertyAttribute generated for a class with attributes ConfigurationProperty

1.7k Views Asked by At

I have an app with a pretty big configuration. All configuration sections for each parameter are defined with .Net ConfigurationProperty attributes, that all have a DefaultValue property.

As our product becomes heavily customizable between countries, and even clients in one country, there is a Configurator.exe that enable to edit the big configuration file.

In this Configurator.exe, it would be really cool if I could have access to the many many many DefaultValue properties that has been defined... However, I don't have the single idea of how I could access those properties generated by the attributes.

e.g.:

public class MyCollection : ConfigurationElementCollection
{
    public MyCollection ()
    {
    }

    [ConfigurationProperty(MyAttr,IsRequired=false,DefaultValue=WantedValue)]
    public MyAttributeType MyAttribute
    {
        //... property implementation
    }
}

What I need is to programmatically access to the value WantedValue, the most generically as possible. (Otherwise I am to manually browse all the ConfigSections defined, collect the DefaultValues for each field, then check my configurator uses these values... )

In fancy it would look like: MyCollection.GetListConfigurationProperty() that would return ConfigurationPropertyAttribute objects on which I could call the Properties : Name, IsRequired, IsKey, IsDefaultCollection, and DefaultValue

Any idea ?

1

There are 1 best solutions below

0
On BEST ANSWER

This is the class I happened to achieve which succeed in what I wanted to do:

I feed it with the ConfigSection type, the type of the defualt value of the field I want, and the string value of the field I want.

public class ExtensionConfigurationElement<TConfigSection, UDefaultValue>
    where UDefaultValue : new() 
    where TConfigSection : ConfigurationElement, new()
{
    public UDefaultValue GetDefaultValue(string strField)
    {
        TConfigSection tConfigSection = new TConfigSection();
        ConfigurationElement configElement = tConfigSection as ConfigurationElement;
        if (configElement == null)
        {
            // not a config section
            System.Diagnostics.Debug.Assert(false);
            return default(UDefaultValue);
        }

        ElementInformation elementInfo = configElement.ElementInformation;

        var varTest = elementInfo.Properties;
        foreach (var item in varTest)
        {
            PropertyInformation propertyInformation = item as PropertyInformation;
            if (propertyInformation == null || propertyInformation.Name != strField)
            {
                continue;
            }

            try
            {
                UDefaultValue defaultValue = (UDefaultValue) propertyInformation.DefaultValue;
                return defaultValue;
            }
            catch (Exception ex)
            {
                // default value of the wrong type
                System.Diagnostics.Debug.Assert(false);
                return default(UDefaultValue);                
            }
        }

        return default(UDefaultValue);
    }
}