1

かなり大きな構成のアプリがあります。各パラメーターのすべての構成セクションは、.Net ConfigurationProperty属性で定義され、すべてDefaultValueプロパティがあります。

私たちの製品が国間で、そしてある国のクライアントでさえも高度にカスタマイズ可能になるにつれて、大きな構成ファイルを編集できるようにするConfigurator.exeがあります。

このConfigurator.exeでは、定義されている非常に多くのDefaultValueプロパティにアクセスできれば、本当にすばらしいでしょう...しかし、によって生成されたこれらのプロパティにアクセスする方法については、私にはわかりません。属性。

例えば:

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

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

私が必要としているのは、可能な限り最も一般的な値であるWantedValueにプログラムでアクセスすることです。(それ以外の場合は、定義されたすべてのConfigSectionsを手動で参照し、各フィールドのDefaultValuesを収集してから、コンフィギュレーターがこれらの値を使用することを確認します...)

ファンシーでは、次のようになります。MyCollection.GetListConfigurationProperty()は、プロパティを呼び出すことができるConfigurationPropertyAttributeオブジェクトを返します:Name、IsRequired、IsKey、IsDefaultCollection、およびDefaultValue

何か案が ?

4

1 に答える 1

0

これは私がたまたま達成したクラスであり、私がやりたかったことで成功しました。

ConfigSectionタイプ、必要なフィールドのデフォルト値のタイプ、および必要なフィールドの文字列値をフィードします。

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);
    }
}
于 2011-09-21T14:23:04.470 に答える