Copied remotely
[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 (!CI.IsStatic)
68                                         throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
69                                 if (arguments == null) // null counts as no arguments
70                                         if (CI.GetParameters().Length != 0)
71                                                 throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
72                                 if (arguments.Count != CI.GetParameters().Length)
73                                         throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
74                                 return true;
75                         case MemberTypes.Method:
76                                 MethodInfo MI = (MethodInfo) member;
77                                 if (!MI.IsStatic)
78                                         throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
79                                 if (arguments == null) // null counts as no arguments
80                                         if (MI.GetParameters().Length != 0)
81                                                 throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
82                                 if (arguments.Count != MI.GetParameters().Length)
83                                         throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
84                                 return true;
85                         case MemberTypes.Field:
86                                 FieldInfo FI = (FieldInfo) member;
87                                 if (!FI.IsStatic)
88                                         throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
89                                 if (arguments == null) // null counts as no arguments
90                                         return true;
91                                 if (arguments.Count == 0)
92                                         throw new ArgumentException ("Field members do not take any arguments", "arguments");
93                                 return true;
94                         case MemberTypes.Property:
95                                 PropertyInfo PI = (PropertyInfo) member;
96                                 if (!(PI.CanRead))
97                                         throw new ArgumentException ("That property cannot be read", "member");
98                                 MethodInfo PIM = PI.GetGetMethod();
99                                 if (!PIM.IsStatic)
100                                         throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
101                                 if (arguments == null) // null counts as no arguments
102                                         if (PIM.GetParameters().Length != 0)
103                                                 throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
104                                 if (arguments.Count != PIM.GetParameters().Length)
105                                         throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
106                                 return true;
107                         }
108                         return false;
109                 }
110
111                 public ICollection Arguments {
112                         get { 
113                                 // It seems MS does not return null even if we specified null as parameter (but does not cause an exception)
114                                 if (arguments == null)
115                                         return new object[0];
116                                 return arguments;
117                         }
118                 }
119
120                 public bool IsComplete {
121                         get { return isComplete; }
122                 }
123
124                 public MemberInfo MemberInfo {
125                         get { return member; }
126                 }
127
128                 public object Invoke()
129                 {
130                         object[] parsearguments;
131                         if (arguments == null)
132                                 parsearguments = new object[0];
133                         else {
134                                 parsearguments = new object[arguments.Count - 1];
135                                 arguments.CopyTo (parsearguments, 0);
136                         }
137
138                         //MemberInfo member;
139                         switch (member.MemberType) {
140                         case MemberTypes.Constructor:
141                                 ConstructorInfo CI = (ConstructorInfo) member;
142                                 return CI.Invoke (parsearguments);
143
144                         case MemberTypes.Method:
145                                 MethodInfo MI = (MethodInfo) member;
146                                 return MI.Invoke (null, parsearguments);
147
148                         case MemberTypes.Field:
149                                 FieldInfo FI = (FieldInfo) member;
150                                 return FI.GetValue (null);
151
152                         case MemberTypes.Property:
153                                 PropertyInfo PI = (PropertyInfo) member;
154                                 return PI.GetValue (null, parsearguments);
155                         }
156                         return null;
157                 }
158         }
159 }