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