ServiceModelConfigurationElement.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.Configuration;
  7. using System.Diagnostics.Contracts;
  8. /// <summary>
  9. /// Binding-related Configuration elements use this base class for WCF-wide commonalities
  10. /// </summary>
  11. public abstract class ServiceModelConfigurationElement : ConfigurationElement
  12. {
  13. /// <summary>
  14. /// Used by InitializeFrom() pattern to avoid writing default values to generated .config files.
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. /// <param name="propertyName">ConfigurationProperty.Name for the configuration property to set</param>
  18. /// <param name="value">Value to set</param>
  19. protected void SetPropertyValueIfNotDefaultValue<T>(string propertyName, T value)
  20. {
  21. var configurationProperty = this.Properties[propertyName];
  22. Contract.Assert(configurationProperty != null, "Parameter 'propertyName' should be the name of a configuration property of type T");
  23. Contract.Assert(configurationProperty.Type.IsAssignableFrom(typeof(T)), "Parameter 'propertyName' should be the name of a configuration property of type T");
  24. if (!object.Equals(value, configurationProperty.DefaultValue))
  25. {
  26. SetPropertyValue(configurationProperty, value, /*ignoreLocks = */ false);
  27. }
  28. }
  29. }
  30. }