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