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;
34 using System.Security.Permissions;
36 namespace System.ComponentModel.Design.Serialization
38 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
39 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;
53 ValidateMember (member, arguments);
55 this.arguments = arguments;
58 private void ValidateMember (MemberInfo member, ICollection arguments)
63 switch (member.MemberType) {
64 // According to docs only these types are allowed, but the docs do
65 // state what happens for other types
66 case MemberTypes.Constructor:
67 ConstructorInfo CI = (ConstructorInfo) member;
68 if (arguments == null) // null counts as no arguments
69 if (CI.GetParameters().Length != 0)
70 throw new ArgumentException ("Invalid number of arguments for this constructor");
71 if (arguments.Count != CI.GetParameters().Length)
72 throw new ArgumentException ("Invalid number of arguments for this constructor");
74 case MemberTypes.Method:
75 MethodInfo MI = (MethodInfo) member;
77 throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
78 if (arguments == null) // null counts as no arguments
79 if (MI.GetParameters().Length != 0)
80 throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
81 if (arguments.Count != MI.GetParameters().Length)
82 throw new ArgumentException ("Invalid number of arguments for this method");
84 case MemberTypes.Field:
85 FieldInfo FI = (FieldInfo) member;
87 throw new ArgumentException ("Parameter must be static");
88 if (arguments != null && arguments.Count != 0) // null counts as no arguments
89 throw new ArgumentException ("Field members do not take any arguments");
91 case MemberTypes.Property:
92 PropertyInfo PI = (PropertyInfo) member;
94 throw new ArgumentException ("Parameter must be readable");
95 MethodInfo PIM = PI.GetGetMethod();
97 throw new ArgumentException ("Parameter must be static");
102 public ICollection Arguments {
104 // It seems MS does not return null even if we specified null as parameter (but does not cause an exception)
105 if (arguments == null)
106 return new object[0];
111 public bool IsComplete {
112 get { return isComplete; }
115 public MemberInfo MemberInfo {
116 get { return member; }
119 public object Invoke()
124 object[] parsearguments;
125 if (arguments == null)
126 parsearguments = new object[0];
128 parsearguments = new object[arguments.Count];
129 arguments.CopyTo (parsearguments, 0);
133 switch (member.MemberType) {
134 case MemberTypes.Constructor:
135 ConstructorInfo CI = (ConstructorInfo) member;
136 return CI.Invoke (parsearguments);
138 case MemberTypes.Method:
139 MethodInfo MI = (MethodInfo) member;
140 return MI.Invoke (null, parsearguments);
142 case MemberTypes.Field:
143 FieldInfo FI = (FieldInfo) member;
144 return FI.GetValue (null);
146 case MemberTypes.Property:
147 PropertyInfo PI = (PropertyInfo) member;
148 return PI.GetValue (null, parsearguments);