2006-02-21 Atsushi Enomoto <atsushi@ximian.com>
[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                 TypeData _typeData;
45                 MemberInfo _member;
46                 MemberInfo _specifiedMember;
47                 object _defaultValue = System.DBNull.Value;
48                 string documentation;
49                 int _flags;
50                 
51                 const int OPTIONAL = 1;
52                 const int RETURN_VALUE = 2;
53                 const int IGNORE = 4;
54
55                 public XmlTypeMapMember()
56                 {
57                 }
58
59                 public string Name
60                 {
61                         get { return _name; }
62                         set { _name = value; }
63                 }
64                 
65                 public object DefaultValue
66                 {
67                         get { return _defaultValue; }
68                         set { _defaultValue = value; }
69                 }
70
71                 public string Documentation
72                 {
73                         set { documentation = value; }
74                         get { return documentation; }
75                 }
76
77                 public bool IsReadOnly (Type type)
78                 {
79                         if (_member == null) InitMember (type);
80                         return (_member is PropertyInfo) && !((PropertyInfo)_member).CanWrite;
81                 }
82
83                 public static object GetValue (object ob, string name)
84                 {
85                         MemberInfo[] mems = ob.GetType().GetMember (name, BindingFlags.Instance|BindingFlags.Public);
86                         if (mems[0] is PropertyInfo) return ((PropertyInfo)mems[0]).GetValue (ob, null);
87                         else return ((FieldInfo)mems[0]).GetValue (ob);
88                 }
89
90                 public object GetValue (object ob)
91                 {
92                         if (_member == null) InitMember (ob.GetType());
93                         if (_member is PropertyInfo) return ((PropertyInfo)_member).GetValue (ob, null);
94                         else return ((FieldInfo)_member).GetValue (ob);
95                 }
96
97                 public void SetValue (object ob, object value)
98                 {
99                         if (_member == null) InitMember (ob.GetType());
100                         if (_member is PropertyInfo) ((PropertyInfo)_member).SetValue (ob, value, null);
101                         else ((FieldInfo)_member).SetValue (ob, value);
102                 }
103
104                 public static void SetValue (object ob, string name, object value)
105                 {
106                         MemberInfo[] mems = ob.GetType().GetMember (name, BindingFlags.Instance|BindingFlags.Public);
107                         if (mems[0] is PropertyInfo) ((PropertyInfo)mems[0]).SetValue (ob, value, null);
108                         else ((FieldInfo)mems[0]).SetValue (ob, value);
109                 }
110
111                 void InitMember (Type type)
112                 {
113                         MemberInfo[] mems = type.GetMember (_name, BindingFlags.Instance|BindingFlags.Public);
114                         _member = mems[0];
115                         
116                         mems = type.GetMember (_name + "Specified", BindingFlags.Instance|BindingFlags.Public);
117                         if (mems.Length > 0) _specifiedMember = mems[0];
118                 }
119
120                 public TypeData TypeData
121                 {
122                         get { return _typeData; }
123                         set { _typeData = value; }
124                 }
125
126                 public int Index
127                 {
128                         get { return _index; }
129                         set { _index = value; }
130                 }
131                 
132                 public bool IsOptionalValueType
133                 {
134                         get { return (_flags & OPTIONAL) != 0; }
135                         set { _flags = value ? (_flags | OPTIONAL) : (_flags & ~OPTIONAL); }
136                 }
137                 
138                 public bool IsReturnValue
139                 {
140                         get { return (_flags & RETURN_VALUE) != 0; }
141                         set { _flags = value ? (_flags | RETURN_VALUE) : (_flags & ~RETURN_VALUE); }
142                 }
143                 
144                 public bool Ignore
145                 {
146                         get { return (_flags & IGNORE) != 0; }
147                         set { _flags = value ? (_flags | IGNORE) : (_flags & ~IGNORE); }
148                 }
149                 
150                 public void CheckOptionalValueType (Type type)
151                 {
152                         if (_member == null) InitMember (type);
153                         IsOptionalValueType = (_specifiedMember != null);
154                 }
155                 
156                 public bool GetValueSpecified (object ob)
157                 {
158                         if (_specifiedMember is PropertyInfo) return (bool) ((PropertyInfo)_specifiedMember).GetValue (ob, null);
159                         else return (bool) ((FieldInfo)_specifiedMember).GetValue (ob);
160                 }
161
162                 public void SetValueSpecified (object ob, bool value)
163                 {
164                         if (_specifiedMember is PropertyInfo) ((PropertyInfo)_specifiedMember).SetValue (ob, value, null);
165                         else ((FieldInfo)_specifiedMember).SetValue (ob, value);
166                 }
167         }
168 }