2 // System.ComponentModel.Design.Serialization.InstanceDescriptor.cs
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) 2003 Martin Willemoes Hansen
9 // (C) 2003 Andreas Nahr
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Reflection;
35 namespace System.ComponentModel.Design.Serialization
37 public sealed class InstanceDescriptor
40 private MemberInfo member;
41 private ICollection arguments;
42 private bool isComplete;
44 public InstanceDescriptor (MemberInfo member, ICollection arguments)
45 : this (member, arguments, true)
49 public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete)
51 this.isComplete = isComplete;
52 if ((member != null) && !IsMemberValid (member, arguments))
53 throw new ArgumentException ("Only Constructor, Method, Field or Property members allowed", "member");
55 this.arguments = arguments;
58 private bool IsMemberValid (MemberInfo member, ICollection arguments)
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");
70 case MemberTypes.Method:
71 MethodInfo MI = (MethodInfo) member;
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");
80 case MemberTypes.Field:
81 FieldInfo FI = (FieldInfo) member;
83 throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
84 if (arguments == null) // null counts as no arguments
86 if (arguments.Count == 0)
87 throw new ArgumentException ("Field members do not take any arguments", "arguments");
89 case MemberTypes.Property:
90 PropertyInfo PI = (PropertyInfo) member;
92 throw new ArgumentException ("That property cannot be read", "member");
93 MethodInfo PIM = PI.GetGetMethod();
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");
106 public ICollection Arguments {
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];
115 public bool IsComplete {
116 get { return isComplete; }
119 public MemberInfo MemberInfo {
120 get { return member; }
123 private bool HasThis ()
125 if (member is ConstructorInfo)
127 MethodInfo mi = (member as MethodInfo);
130 FieldInfo fi = (member as FieldInfo);
133 PropertyInfo pi = (member as PropertyInfo);
135 return !pi.GetGetMethod ().IsStatic;
139 public object Invoke()
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);
148 parsearguments = (arguments as object[]);
149 if (parsearguments == null) {
150 parsearguments = new object[arguments.Count];
151 arguments.CopyTo (parsearguments, 0);
156 switch (member.MemberType) {
157 case MemberTypes.Constructor:
158 ConstructorInfo CI = (ConstructorInfo) member;
159 return CI.Invoke (parsearguments);
161 case MemberTypes.Method:
162 MethodInfo MI = (MethodInfo) member;
163 return MI.Invoke (null, parsearguments);
165 case MemberTypes.Field:
166 FieldInfo FI = (FieldInfo) member;
167 return FI.GetValue (null);
169 case MemberTypes.Property:
170 PropertyInfo PI = (PropertyInfo) member;
171 return PI.GetValue (null, parsearguments);