6e75bd45ff53e66395d5f25b6e7d812acded3bed
[mono.git] / mcs / class / System / System.ComponentModel.Design.Serialization / InstanceDescriptor.cs
1 //
2 // System.ComponentModel.Design.Serialization.InstanceDescriptor.cs
3 //
4 // Authors:
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2003 Andreas Nahr
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.Reflection;
34
35 namespace System.ComponentModel.Design.Serialization
36 {
37         public sealed class InstanceDescriptor
38         {
39
40                 private MemberInfo member;
41                 private ICollection arguments;
42                 private bool isComplete;
43
44                 public InstanceDescriptor (MemberInfo member, ICollection arguments)
45                         : this (member, arguments, true)
46                 {
47                 }
48
49                 public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete)
50                 {
51                         this.isComplete = isComplete;
52                         if ((member != null) && !IsMemberValid (member, arguments))
53                                 throw new ArgumentException ("Only Constructor, Method, Field or Property members allowed", "member");
54                         this.member = member;
55                         this.arguments = arguments;
56                 }
57
58                 private bool IsMemberValid (MemberInfo member, ICollection arguments)
59                 {
60                         switch (member.MemberType) {
61                         // According to docs only these types are allowed
62                         case MemberTypes.Constructor:
63                                 ConstructorInfo CI = (ConstructorInfo) member;
64                                 if (arguments == null) // null counts as no arguments
65                                         if (CI.GetParameters().Length != 0)
66                                                 throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
67                                 if (arguments.Count != CI.GetParameters().Length)
68                                         throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
69                                 return true;
70                         case MemberTypes.Method:
71                                 MethodInfo MI = (MethodInfo) member;
72                                 if (!MI.IsStatic)
73                                         throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
74                                 if (arguments == null) // null counts as no arguments
75                                         if (MI.GetParameters().Length != 0)
76                                                 throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
77                                 if (arguments.Count != MI.GetParameters().Length)
78                                         throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
79                                 return true;
80                         case MemberTypes.Field:
81                                 FieldInfo FI = (FieldInfo) member;
82                                 if (!FI.IsStatic)
83                                         throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
84                                 if (arguments == null) // null counts as no arguments
85                                         return true;
86                                 if (arguments.Count == 0)
87                                         throw new ArgumentException ("Field members do not take any arguments", "arguments");
88                                 return true;
89                         case MemberTypes.Property:
90                                 PropertyInfo PI = (PropertyInfo) member;
91                                 if (!(PI.CanRead))
92                                         throw new ArgumentException ("That property cannot be read", "member");
93                                 MethodInfo PIM = PI.GetGetMethod();
94                                 if (!PIM.IsStatic)
95                                         throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
96                                 if (arguments == null) // null counts as no arguments
97                                         if (PIM.GetParameters().Length != 0)
98                                                 throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
99                                 if (arguments.Count != PIM.GetParameters().Length)
100                                         throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
101                                 return true;
102                         }
103                         return false;
104                 }
105
106                 public ICollection Arguments {
107                         get { 
108                                 // It seems MS does not return null even if we specified null as parameter (but does not cause an exception)
109                                 if (arguments == null)
110                                         return new object[0];
111                                 return arguments;
112                         }
113                 }
114
115                 public bool IsComplete {
116                         get { return isComplete; }
117                 }
118
119                 public MemberInfo MemberInfo {
120                         get { return member; }
121                 }
122
123                 private bool HasThis ()
124                 {
125                         if (member is ConstructorInfo)
126                                 return false;
127                         MethodInfo mi = (member as MethodInfo);
128                         if (mi != null)
129                                 return !mi.IsStatic;
130                         FieldInfo fi = (member as FieldInfo);
131                         if (fi != null)
132                                 return !fi.IsStatic;
133                         PropertyInfo pi = (member as PropertyInfo);
134                         if (pi != null)
135                                 return !pi.GetGetMethod ().IsStatic;
136                         return true;
137                 }
138
139                 public object Invoke()
140                 {
141                         object[] parsearguments;
142                         if (arguments == null)
143                                 parsearguments = new object[0];
144                         else if (HasThis ()) {
145                                 parsearguments = new object[arguments.Count - 1];
146                                 arguments.CopyTo (parsearguments, 0);
147                         } else {
148                                 parsearguments = (arguments as object[]);
149                                 if (parsearguments == null) {
150                                         parsearguments = new object[arguments.Count];
151                                         arguments.CopyTo (parsearguments, 0);
152                                 }
153                         }
154
155                         //MemberInfo member;
156                         switch (member.MemberType) {
157                         case MemberTypes.Constructor:
158                                 ConstructorInfo CI = (ConstructorInfo) member;
159                                 return CI.Invoke (parsearguments);
160
161                         case MemberTypes.Method:
162                                 MethodInfo MI = (MethodInfo) member;
163                                 return MI.Invoke (null, parsearguments);
164
165                         case MemberTypes.Field:
166                                 FieldInfo FI = (FieldInfo) member;
167                                 return FI.GetValue (null);
168
169                         case MemberTypes.Property:
170                                 PropertyInfo PI = (PropertyInfo) member;
171                                 return PI.GetValue (null, parsearguments);
172                         }
173                         return null;
174                 }
175         }
176 }