New test.
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlTypeMapMember.cs
1 //
2 // XmlTypeMapMember.cs: 
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (C) 2002, 2003 Ximian, Inc.  http://www.ximian.com
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;
32 using System.Collections;
33 using System.Reflection;
34
35 namespace System.Xml.Serialization
36 {
37         // XmlTypeMapMember
38         // A member of a class that must be serialized
39
40         internal class XmlTypeMapMember
41         {
42                 string _name;
43                 int _index;
44                 int _globalIndex;
45                 TypeData _typeData;
46                 MemberInfo _member;
47                 MemberInfo _specifiedMember;
48                 object _defaultValue = System.DBNull.Value;
49                 string documentation;
50                 int _flags;
51                 
52                 const int OPTIONAL = 1;
53                 const int RETURN_VALUE = 2;
54                 const int IGNORE = 4;
55
56                 public XmlTypeMapMember()
57                 {
58                 }
59
60                 public string Name
61                 {
62                         get { return _name; }
63                         set { _name = value; }
64                 }
65                 
66                 public object DefaultValue
67                 {
68                         get { return _defaultValue; }
69                         set { _defaultValue = value; }
70                 }
71
72                 public string Documentation
73                 {
74                         set { documentation = value; }
75                         get { return documentation; }
76                 }
77
78                 public bool IsReadOnly (Type type)
79                 {
80                         if (_member == null) InitMember (type);
81                         return (_member is PropertyInfo) && !((PropertyInfo)_member).CanWrite;
82                 }
83
84                 public static object GetValue (object ob, string name)
85                 {
86                         MemberInfo[] mems = ob.GetType().GetMember (name, BindingFlags.Instance|BindingFlags.Public);
87                         if (mems[0] is PropertyInfo) return ((PropertyInfo)mems[0]).GetValue (ob, null);
88                         else return ((FieldInfo)mems[0]).GetValue (ob);
89                 }
90
91                 public object GetValue (object ob)
92                 {
93                         if (_member == null) InitMember (ob.GetType());
94                         if (_member is PropertyInfo) return ((PropertyInfo)_member).GetValue (ob, null);
95                         else return ((FieldInfo)_member).GetValue (ob);
96                 }
97
98                 public void SetValue (object ob, object value)
99                 {
100                         if (_member == null) InitMember (ob.GetType());
101                         if (_member is PropertyInfo) ((PropertyInfo)_member).SetValue (ob, value, null);
102                         else ((FieldInfo)_member).SetValue (ob, value);
103                 }
104
105                 public static void SetValue (object ob, string name, object value)
106                 {
107                         MemberInfo[] mems = ob.GetType().GetMember (name, BindingFlags.Instance|BindingFlags.Public);
108                         if (mems[0] is PropertyInfo) ((PropertyInfo)mems[0]).SetValue (ob, value, null);
109                         else ((FieldInfo)mems[0]).SetValue (ob, value);
110                 }
111
112                 void InitMember (Type type)
113                 {
114                         MemberInfo[] mems = type.GetMember (_name, BindingFlags.Instance|BindingFlags.Public);
115                         _member = mems[0];
116                         
117                         mems = type.GetMember (_name + "Specified", BindingFlags.Instance|BindingFlags.Public);
118                         if (mems.Length > 0) _specifiedMember = mems[0];
119                 }
120
121                 public TypeData TypeData
122                 {
123                         get { return _typeData; }
124                         set { _typeData = value; }
125                 }
126
127                 public int Index
128                 {
129                         get { return _index; }
130                         set { _index = value; }
131                 }
132                 
133                 public int GlobalIndex
134                 {
135                         get { return _globalIndex; }
136                         set { _globalIndex = value; }
137                 }
138                 
139                 public bool IsOptionalValueType
140                 {
141                         get { return (_flags & OPTIONAL) != 0; }
142                         set { _flags = value ? (_flags | OPTIONAL) : (_flags & ~OPTIONAL); }
143                 }
144                 
145                 public bool IsReturnValue
146                 {
147                         get { return (_flags & RETURN_VALUE) != 0; }
148                         set { _flags = value ? (_flags | RETURN_VALUE) : (_flags & ~RETURN_VALUE); }
149                 }
150                 
151                 public bool Ignore
152                 {
153                         get { return (_flags & IGNORE) != 0; }
154                         set { _flags = value ? (_flags | IGNORE) : (_flags & ~IGNORE); }
155                 }
156                 
157                 public void CheckOptionalValueType (Type type)
158                 {
159                         if (_member == null) InitMember (type);
160                         IsOptionalValueType = (_specifiedMember != null);
161                 }
162                 
163                 public bool GetValueSpecified (object ob)
164                 {
165                         if (_specifiedMember is PropertyInfo) return (bool) ((PropertyInfo)_specifiedMember).GetValue (ob, null);
166                         else return (bool) ((FieldInfo)_specifiedMember).GetValue (ob);
167                 }
168
169                 public void SetValueSpecified (object ob, bool value)
170                 {
171                         if (_specifiedMember is PropertyInfo) ((PropertyInfo)_specifiedMember).SetValue (ob, value, null);
172                         else ((FieldInfo)_specifiedMember).SetValue (ob, value);
173                 }
174         }
175 }