SoapHeaderMapping.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // SoapHeaderMapping.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Reflection;
  30. namespace System.Web.Services.Protocols
  31. {
  32. #if NET_2_0
  33. public
  34. #else
  35. internal
  36. #endif
  37. sealed class SoapHeaderMapping // It used to be HeaderInfo class until Mono 1.2
  38. {
  39. MemberInfo member;
  40. Type header_type;
  41. bool is_unknown_header;
  42. SoapHeaderDirection direction;
  43. internal SoapHeaderMapping (MemberInfo member, SoapHeaderAttribute attributeInfo)
  44. {
  45. this.member = member;
  46. direction = attributeInfo.Direction;
  47. if (member is PropertyInfo)
  48. header_type = ((PropertyInfo) member).PropertyType;
  49. else
  50. header_type = ((FieldInfo) member).FieldType;
  51. if (HeaderType == typeof(SoapHeader) || HeaderType == typeof(SoapUnknownHeader) ||
  52. HeaderType == typeof(SoapHeader[]) || HeaderType == typeof(SoapUnknownHeader[]))
  53. {
  54. is_unknown_header = true;
  55. }
  56. else if (!typeof(SoapHeader).IsAssignableFrom (HeaderType))
  57. throw new InvalidOperationException (string.Format ("Header members type must be a SoapHeader subclass"));
  58. }
  59. internal object GetHeaderValue (object ob)
  60. {
  61. if (member is PropertyInfo)
  62. return ((PropertyInfo) member).GetValue (ob, null);
  63. else
  64. return ((FieldInfo) member).GetValue (ob);
  65. }
  66. internal void SetHeaderValue (object ob, SoapHeader header)
  67. {
  68. object value = header;
  69. if (Custom && HeaderType.IsArray)
  70. {
  71. SoapUnknownHeader uheader = header as SoapUnknownHeader;
  72. SoapUnknownHeader[] array = (SoapUnknownHeader[]) GetHeaderValue (ob);
  73. if (array == null || array.Length == 0) {
  74. value = new SoapUnknownHeader[] { uheader };
  75. }
  76. else {
  77. SoapUnknownHeader[] newArray = new SoapUnknownHeader [array.Length+1];
  78. Array.Copy (array, newArray, array.Length);
  79. newArray [array.Length] = uheader;
  80. value = newArray;
  81. }
  82. }
  83. if (member is PropertyInfo)
  84. ((PropertyInfo) member).SetValue (ob, value, null);
  85. else
  86. ((FieldInfo) member).SetValue (ob, value);
  87. }
  88. public SoapHeaderDirection Direction
  89. {
  90. get { return direction; }
  91. }
  92. public MemberInfo MemberInfo {
  93. get { return member; }
  94. }
  95. public Type HeaderType {
  96. get { return header_type; }
  97. }
  98. public bool Custom {
  99. get { return is_unknown_header; }
  100. }
  101. [MonoTODO]
  102. public bool Repeats {
  103. get { throw new NotImplementedException (); }
  104. }
  105. }
  106. }