Merge branch 'patch-1' of https://github.com/ReubenBond/mono into ReubenBond-patch-1
[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 #if NET_2_0
36         public
37 #else
38         internal
39 #endif
40         sealed class SoapHeaderMapping // It used to be HeaderInfo class until Mono 1.2
41         {
42                 MemberInfo member;
43                 Type header_type;
44                 bool is_unknown_header;
45                 SoapHeaderDirection direction;
46
47                 internal SoapHeaderMapping (MemberInfo member, SoapHeaderAttribute attributeInfo)
48                 {
49                         this.member = member;
50                         direction = attributeInfo.Direction;
51                         if (member is PropertyInfo)
52                                 header_type = ((PropertyInfo) member).PropertyType;
53                         else
54                                 header_type = ((FieldInfo) member).FieldType;
55                         
56                         if (HeaderType == typeof(SoapHeader) || HeaderType == typeof(SoapUnknownHeader) ||
57                                 HeaderType == typeof(SoapHeader[]) || HeaderType == typeof(SoapUnknownHeader[]))
58                         {
59                                 is_unknown_header = true;
60                         }
61                         else if (!typeof(SoapHeader).IsAssignableFrom (HeaderType))
62                                 throw new InvalidOperationException (string.Format ("Header members type must be a SoapHeader subclass"));
63                 }
64                 
65                 internal object GetHeaderValue (object ob)
66                 {
67                         if (member is PropertyInfo)
68                                 return ((PropertyInfo) member).GetValue (ob, null);
69                         else
70                                 return ((FieldInfo) member).GetValue (ob);
71                 }
72
73                 internal void SetHeaderValue (object ob, SoapHeader header)
74                 {
75                         object value = header;
76                         if (Custom && HeaderType.IsArray)
77                         {
78                                 SoapUnknownHeader uheader = header as SoapUnknownHeader;
79                                 SoapUnknownHeader[] array = (SoapUnknownHeader[]) GetHeaderValue (ob);
80                                 if (array == null || array.Length == 0) {
81                                         value = new SoapUnknownHeader[] { uheader };
82                                 }
83                                 else {
84                                         SoapUnknownHeader[] newArray = new SoapUnknownHeader [array.Length+1];
85                                         Array.Copy (array, newArray, array.Length);
86                                         newArray [array.Length] = uheader;
87                                         value = newArray;
88                                 }
89                         }
90                         
91                         if (member is PropertyInfo)
92                                 ((PropertyInfo) member).SetValue (ob, value, null);
93                         else
94                                 ((FieldInfo) member).SetValue (ob, value);
95                 }
96                 
97                 public SoapHeaderDirection Direction
98                 {
99                         get { return direction; }
100                 }
101
102                 public MemberInfo MemberInfo {
103                         get { return member; }
104                 }
105
106                 public Type HeaderType {
107                         get { return header_type; }
108                 }
109
110                 public bool Custom {
111                         get { return is_unknown_header; }
112                 }
113
114                 [MonoTODO]
115                 public bool Repeats {
116                         get { throw new NotImplementedException (); }
117                 }
118         }
119
120 }