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
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Reflection;
36 namespace System.ComponentModel.Design.Serialization
38 public sealed class InstanceDescriptor
41 private MemberInfo member;
42 private ICollection arguments;
43 private bool isComplete;
45 public InstanceDescriptor (MemberInfo member, ICollection arguments)
46 : this (member, arguments, true)
50 public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete)
52 this.isComplete = isComplete;
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");
58 this.arguments = arguments;
61 private bool IsMemberValid (MemberInfo member, ICollection arguments)
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");
73 case MemberTypes.Method:
74 MethodInfo MI = (MethodInfo) member;
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");
83 case MemberTypes.Field:
84 FieldInfo FI = (FieldInfo) member;
86 throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
87 if (arguments == null) // null counts as no arguments
89 if (arguments.Count == 0)
90 throw new ArgumentException ("Field members do not take any arguments", "arguments");
92 case MemberTypes.Property:
93 PropertyInfo PI = (PropertyInfo) member;
95 throw new ArgumentException ("That property cannot be read", "member");
96 MethodInfo PIM = PI.GetGetMethod();
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");
109 public ICollection Arguments {
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];
118 public bool IsComplete {
119 get { return isComplete; }
122 public MemberInfo MemberInfo {
123 get { return member; }
126 public object Invoke()
128 object[] parsearguments;
129 if (arguments == null)
130 parsearguments = new object[0];
132 parsearguments = new object[arguments.Count - 1];
133 arguments.CopyTo (parsearguments, 0);
137 switch (member.MemberType) {
138 case MemberTypes.Constructor:
139 ConstructorInfo CI = (ConstructorInfo) member;
140 return CI.Invoke (parsearguments);
142 case MemberTypes.Method:
143 MethodInfo MI = (MethodInfo) member;
144 return MI.Invoke (null, parsearguments);
146 case MemberTypes.Field:
147 FieldInfo FI = (FieldInfo) member;
148 return FI.GetValue (null);
150 case MemberTypes.Property:
151 PropertyInfo PI = (PropertyInfo) member;
152 return PI.GetValue (null, parsearguments);