ParameterElement.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.Runtime.Serialization.Configuration
  5. {
  6. using System;
  7. using System.Configuration;
  8. using System.Xml;
  9. using System.Security;
  10. public sealed partial class ParameterElement : ConfigurationElement
  11. {
  12. public ParameterElement()
  13. {
  14. }
  15. public ParameterElement(string typeName)
  16. : this()
  17. {
  18. if (String.IsNullOrEmpty(typeName))
  19. {
  20. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("typeName");
  21. }
  22. this.Type = typeName;
  23. }
  24. public ParameterElement(int index)
  25. : this()
  26. {
  27. this.Index = index;
  28. }
  29. [ConfigurationProperty(ConfigurationStrings.Index, DefaultValue = 0)]
  30. [IntegerValidator(MinValue = 0)]
  31. public int Index
  32. {
  33. get { return (int)base[ConfigurationStrings.Index]; }
  34. set { base[ConfigurationStrings.Index] = value; }
  35. }
  36. [ConfigurationProperty(ConfigurationStrings.DefaultCollectionName, DefaultValue = null, Options = ConfigurationPropertyOptions.IsDefaultCollection)]
  37. public ParameterElementCollection Parameters
  38. {
  39. get { return (ParameterElementCollection)base[ConfigurationStrings.DefaultCollectionName]; }
  40. }
  41. protected override void PostDeserialize()
  42. {
  43. this.Validate();
  44. }
  45. protected override void PreSerialize(XmlWriter writer)
  46. {
  47. this.Validate();
  48. }
  49. [ConfigurationProperty(ConfigurationStrings.Type, DefaultValue = "")]
  50. [StringValidator(MinLength = 0)]
  51. public string Type
  52. {
  53. get { return (string)base[ConfigurationStrings.Type]; }
  54. set
  55. {
  56. if (String.IsNullOrEmpty(value))
  57. {
  58. value = String.Empty;
  59. }
  60. base[ConfigurationStrings.Type] = value;
  61. }
  62. }
  63. void Validate()
  64. {
  65. PropertyInformationCollection propertyInfo = this.ElementInformation.Properties;
  66. if ((propertyInfo[ConfigurationStrings.Index].ValueOrigin == PropertyValueOrigin.Default) &&
  67. (propertyInfo[ConfigurationStrings.Type].ValueOrigin == PropertyValueOrigin.Default))
  68. {
  69. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  70. SR.GetString(SR.ConfigMustSetTypeOrIndex)));
  71. }
  72. if ((propertyInfo[ConfigurationStrings.Index].ValueOrigin != PropertyValueOrigin.Default) &&
  73. (propertyInfo[ConfigurationStrings.Type].ValueOrigin != PropertyValueOrigin.Default))
  74. {
  75. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  76. SR.GetString(SR.ConfigMustOnlySetTypeOrIndex)));
  77. }
  78. if ((propertyInfo[ConfigurationStrings.Index].ValueOrigin != PropertyValueOrigin.Default) && this.Parameters.Count > 0)
  79. {
  80. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
  81. SR.GetString(SR.ConfigMustOnlyAddParamsWithType)));
  82. }
  83. }
  84. internal readonly Guid identity = Guid.NewGuid();
  85. [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview - Loads type given name in configuration."
  86. + " Since this information is used to determine whether a particular type is included as a known type,"
  87. + " changes to the logic should be reviewed.")]
  88. internal Type GetType(string rootType, Type[] typeArgs)
  89. {
  90. return TypeElement.GetType(rootType, typeArgs, this.Type, this.Index, this.Parameters);
  91. }
  92. }
  93. }