add ExcludeFromCodeCoverageAttribute
[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 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.Collections;
33 using System.Reflection;
34 using System.Security.Permissions;
35
36 namespace System.ComponentModel.Design.Serialization
37 {
38         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
39         public sealed class InstanceDescriptor {
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                         ValidateMember (member, arguments);
54                         this.member = member;
55                         this.arguments = arguments;
56                 }
57
58                 private void ValidateMember (MemberInfo member, ICollection arguments)
59                 {
60                         if (member == null)
61                                 return;
62
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");
73                                 break;
74                         case MemberTypes.Method:
75                                 MethodInfo MI = (MethodInfo) member;
76                                 if (!MI.IsStatic)
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");
83                                 break;
84                         case MemberTypes.Field:
85                                 FieldInfo FI = (FieldInfo) member;
86                                 if (!FI.IsStatic)
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");
90                                 break;
91                         case MemberTypes.Property:
92                                 PropertyInfo PI = (PropertyInfo) member;
93                                 if (!(PI.CanRead))
94                                         throw new ArgumentException ("Parameter must be readable");
95                                 MethodInfo PIM = PI.GetGetMethod();
96                                 if (!PIM.IsStatic)
97                                         throw new ArgumentException ("Parameter must be static");
98                                 break;
99                         }
100                 }
101
102                 public ICollection Arguments {
103                         get { 
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];
107                                 return arguments;
108                         }
109                 }
110
111                 public bool IsComplete {
112                         get { return isComplete; }
113                 }
114
115                 public MemberInfo MemberInfo {
116                         get { return member; }
117                 }
118
119                 public object Invoke()
120                 {
121                         if (member == null)
122                                 return null;
123
124                         object[] parsearguments;
125                         if (arguments == null)
126                                 parsearguments = new object[0];
127                         else {
128                                 parsearguments = new object[arguments.Count];
129                                 arguments.CopyTo (parsearguments, 0);
130                         }
131
132                         //MemberInfo member;
133                         switch (member.MemberType) {
134                         case MemberTypes.Constructor:
135                                 ConstructorInfo CI = (ConstructorInfo) member;
136                                 return CI.Invoke (parsearguments);
137
138                         case MemberTypes.Method:
139                                 MethodInfo MI = (MethodInfo) member;
140                                 return MI.Invoke (null, parsearguments);
141
142                         case MemberTypes.Field:
143                                 FieldInfo FI = (FieldInfo) member;
144                                 return FI.GetValue (null);
145
146                         case MemberTypes.Property:
147                                 PropertyInfo PI = (PropertyInfo) member;
148                                 return PI.GetValue (null, parsearguments);
149                         }
150                         return null;
151                 }
152         }
153 }