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
;
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");
75 case MemberTypes
.Method
:
76 MethodInfo MI
= (MethodInfo
) member
;
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");
85 case MemberTypes
.Field
:
86 FieldInfo FI
= (FieldInfo
) member
;
88 throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
89 if (arguments
== null) // null counts as no arguments
91 if (arguments
.Count
== 0)
92 throw new ArgumentException ("Field members do not take any arguments", "arguments");
94 case MemberTypes
.Property
:
95 PropertyInfo PI
= (PropertyInfo
) member
;
97 throw new ArgumentException ("That property cannot be read", "member");
98 MethodInfo PIM
= PI
.GetGetMethod();
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");
111 public ICollection Arguments
{
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];
120 public bool IsComplete
{
121 get { return isComplete; }
124 public MemberInfo MemberInfo
{
125 get { return member; }
128 public object Invoke()
130 object[] parsearguments
;
131 if (arguments
== null)
132 parsearguments
= new object[0];
134 parsearguments
= new object[arguments
.Count
- 1];
135 arguments
.CopyTo (parsearguments
, 0);
139 switch (member
.MemberType
) {
140 case MemberTypes
.Constructor
:
141 ConstructorInfo CI
= (ConstructorInfo
) member
;
142 return CI
.Invoke (parsearguments
);
144 case MemberTypes
.Method
:
145 MethodInfo MI
= (MethodInfo
) member
;
146 return MI
.Invoke (null, parsearguments
);
148 case MemberTypes
.Field
:
149 FieldInfo FI
= (FieldInfo
) member
;
150 return FI
.GetValue (null);
152 case MemberTypes
.Property
:
153 PropertyInfo PI
= (PropertyInfo
) member
;
154 return PI
.GetValue (null, parsearguments
);