示例#1
0
 static internal ArgumentException PropertyNullOrEmpty(string property)
 {
     return(new ArgumentException(SR.GetString(SR.Property_NullOrEmpty, property), property));
 }
示例#2
0
 static internal ArgumentException PropertyInvalid(string property)
 {
     return(new ArgumentException(SR.GetString(SR.Property_Invalid, property), property));
 }
示例#3
0
 static internal ArgumentException ParameterInvalid(string parameter)
 {
     return(new ArgumentException(SR.GetString(SR.Parameter_Invalid, parameter), parameter));
 }
示例#4
0
 static internal ArgumentException ParameterNullOrEmpty(string parameter)
 {
     return(new ArgumentException(SR.GetString(SR.Parameter_NullOrEmpty, parameter), parameter));
 }
示例#5
0
        /// <summary>
        /// Creates a SettingsProperty object using the metadata on the given property
        /// and returns it.
        /// </summary>
        private SettingsProperty CreateSetting(PropertyInfo propertyInfo)
        {
            // Initialization method -
            // be careful not to access properties here to prevent stack overflow.

            object[]         attributes        = propertyInfo.GetCustomAttributes(false);
            SettingsProperty settingsProperty  = new SettingsProperty(Initializer);
            bool             explicitSerialize = _explicitSerializeOnClass;

            settingsProperty.Name         = propertyInfo.Name;
            settingsProperty.PropertyType = propertyInfo.PropertyType;

            for (int i = 0; i < attributes.Length; i++)
            {
                Attribute attribute = attributes[i] as Attribute;
                if (attribute == null)
                {
                    continue;
                }

                if (attribute is DefaultSettingValueAttribute)
                {
                    settingsProperty.DefaultValue = ((DefaultSettingValueAttribute)attribute).Value;
                }
                else if (attribute is ReadOnlyAttribute)
                {
                    settingsProperty.IsReadOnly = true;
                }
                else if (attribute is SettingsProviderAttribute)
                {
                    string providerTypeName = ((SettingsProviderAttribute)attribute).ProviderTypeName;
                    Type   providerType     = Type.GetType(providerTypeName);
                    if (providerType == null)
                    {
                        throw new ConfigurationErrorsException(SR.Format(SR.ProviderTypeLoadFailed, providerTypeName));
                    }

                    SettingsProvider settingsProvider = TypeUtil.CreateInstance(providerType) as SettingsProvider;

                    if (settingsProvider == null)
                    {
                        throw new ConfigurationErrorsException(SR.Format(SR.ProviderInstantiationFailed, providerTypeName));
                    }

                    settingsProvider.Initialize(null, null);
                    settingsProvider.ApplicationName = ConfigurationManagerInternalFactory.Instance.ExeProductName;

                    // See if we already have a provider of the same name in our collection. If so,
                    // re-use the existing instance, since we cannot have multiple providers of the same name.
                    SettingsProvider existing = _providers[settingsProvider.Name];
                    if (existing != null)
                    {
                        settingsProvider = existing;
                    }

                    settingsProperty.Provider = settingsProvider;
                }
                else if (attribute is SettingsSerializeAsAttribute)
                {
                    settingsProperty.SerializeAs = ((SettingsSerializeAsAttribute)attribute).SerializeAs;
                    explicitSerialize            = true;
                }
                else
                {
                    // This isn't an attribute we care about, so simply pass it on
                    // to the SettingsProvider.
                    //
                    // NOTE: The key is the type. So if an attribute was found at class
                    //       level and also property level, the latter overrides the former
                    //       for a given setting. This is exactly the behavior we want.

                    settingsProperty.Attributes.Add(attribute.GetType(), attribute);
                }
            }

            if (!explicitSerialize)
            {
                // Serialization method was not explicitly attributed.

                TypeConverter tc = TypeDescriptor.GetConverter(propertyInfo.PropertyType);
                if (tc.CanConvertTo(typeof(string)) && tc.CanConvertFrom(typeof(string)))
                {
                    // We can use string
                    settingsProperty.SerializeAs = SettingsSerializeAs.String;
                }
                else
                {
                    // Fallback is Xml
                    settingsProperty.SerializeAs = SettingsSerializeAs.Xml;
                }
            }

            return(settingsProperty);
        }