StandardBindingCollectionElement.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Configuration;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Channels;
  11. public partial class StandardBindingCollectionElement<TStandardBinding, TBindingConfiguration> : BindingCollectionElement
  12. where TStandardBinding : Binding
  13. where TBindingConfiguration : StandardBindingElement, new()
  14. {
  15. [ConfigurationProperty(ConfigurationStrings.DefaultCollectionName, Options = ConfigurationPropertyOptions.IsDefaultCollection)]
  16. public StandardBindingElementCollection<TBindingConfiguration> Bindings
  17. {
  18. get { return (StandardBindingElementCollection<TBindingConfiguration>)base[ConfigurationStrings.DefaultCollectionName]; }
  19. }
  20. public override Type BindingType
  21. {
  22. get { return typeof(TStandardBinding); }
  23. }
  24. public override ReadOnlyCollection<IBindingConfigurationElement> ConfiguredBindings
  25. {
  26. get
  27. {
  28. List<IBindingConfigurationElement> configuredBindings = new List<IBindingConfigurationElement>();
  29. foreach (IBindingConfigurationElement configuredBinding in this.Bindings)
  30. {
  31. configuredBindings.Add(configuredBinding);
  32. }
  33. return new ReadOnlyCollection<IBindingConfigurationElement>(configuredBindings);
  34. }
  35. }
  36. public override bool ContainsKey(string name)
  37. {
  38. // This line needed because of the IBindingSection implementation
  39. StandardBindingCollectionElement<TStandardBinding, TBindingConfiguration> me = (StandardBindingCollectionElement<TStandardBinding, TBindingConfiguration>)this;
  40. #pragma warning suppress 56506 //[....]; me.Bindings can never be null (underlying configuration system guarantees)
  41. return me.Bindings.ContainsKey(name);
  42. }
  43. protected internal override Binding GetDefault()
  44. {
  45. return System.Activator.CreateInstance<TStandardBinding>();
  46. }
  47. protected internal override bool TryAdd(string name, Binding binding, Configuration config)
  48. {
  49. // The configuration item needs to understand the BindingType && be of type CustomBindingConfigurationElement
  50. // or StandardBindingConfigurationElement
  51. bool retval = (binding.GetType() == typeof(TStandardBinding)) &&
  52. typeof(StandardBindingElement).IsAssignableFrom(typeof(TBindingConfiguration));
  53. if (retval)
  54. {
  55. TBindingConfiguration bindingConfig = new TBindingConfiguration();
  56. bindingConfig.Name = name;
  57. bindingConfig.InitializeFrom(binding);
  58. this.Bindings.Add(bindingConfig);
  59. }
  60. return retval;
  61. }
  62. }
  63. }