InstanceDescriptor.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // System.ComponentModel.Design.Serialization.InstanceDescriptor.cs
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Andreas Nahr ([email protected])
  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. using System.Collections;
  32. using System.Reflection;
  33. using System.Security.Permissions;
  34. namespace System.ComponentModel.Design.Serialization {
  35. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  36. public sealed class InstanceDescriptor {
  37. private MemberInfo member;
  38. private ICollection arguments;
  39. private bool isComplete;
  40. public InstanceDescriptor (MemberInfo member, ICollection arguments)
  41. : this (member, arguments, true)
  42. {
  43. }
  44. public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete)
  45. {
  46. this.isComplete = isComplete;
  47. if ((member != null) && !IsMemberValid (member, arguments))
  48. throw new ArgumentException ("Only Constructor, Method, Field or Property members allowed", "member");
  49. this.member = member;
  50. this.arguments = arguments;
  51. }
  52. private bool IsMemberValid (MemberInfo member, ICollection arguments)
  53. {
  54. switch (member.MemberType) {
  55. // According to docs only these types are allowed
  56. case MemberTypes.Constructor:
  57. ConstructorInfo CI = (ConstructorInfo) member;
  58. if (arguments == null) // null counts as no arguments
  59. if (CI.GetParameters().Length != 0)
  60. throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
  61. if (arguments.Count != CI.GetParameters().Length)
  62. throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
  63. return true;
  64. case MemberTypes.Method:
  65. MethodInfo MI = (MethodInfo) member;
  66. if (!MI.IsStatic)
  67. throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
  68. if (arguments == null) // null counts as no arguments
  69. if (MI.GetParameters().Length != 0)
  70. throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
  71. if (arguments.Count != MI.GetParameters().Length)
  72. throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
  73. return true;
  74. case MemberTypes.Field:
  75. FieldInfo FI = (FieldInfo) member;
  76. if (!FI.IsStatic)
  77. throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
  78. if (arguments == null) // null counts as no arguments
  79. return true;
  80. if (arguments.Count == 0)
  81. throw new ArgumentException ("Field members do not take any arguments", "arguments");
  82. return true;
  83. case MemberTypes.Property:
  84. PropertyInfo PI = (PropertyInfo) member;
  85. if (!(PI.CanRead))
  86. throw new ArgumentException ("That property cannot be read", "member");
  87. MethodInfo PIM = PI.GetGetMethod();
  88. if (!PIM.IsStatic)
  89. throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
  90. if (arguments == null) // null counts as no arguments
  91. if (PIM.GetParameters().Length != 0)
  92. throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
  93. if (arguments.Count != PIM.GetParameters().Length)
  94. throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
  95. return true;
  96. }
  97. return false;
  98. }
  99. public ICollection Arguments {
  100. get {
  101. // It seems MS does not return null even if we specified null as parameter (but does not cause an exception)
  102. if (arguments == null)
  103. return new object[0];
  104. return arguments;
  105. }
  106. }
  107. public bool IsComplete {
  108. get { return isComplete; }
  109. }
  110. public MemberInfo MemberInfo {
  111. get { return member; }
  112. }
  113. private bool HasThis ()
  114. {
  115. if (member is ConstructorInfo)
  116. return false;
  117. MethodInfo mi = (member as MethodInfo);
  118. if (mi != null)
  119. return !mi.IsStatic;
  120. FieldInfo fi = (member as FieldInfo);
  121. if (fi != null)
  122. return !fi.IsStatic;
  123. PropertyInfo pi = (member as PropertyInfo);
  124. if (pi != null)
  125. return !pi.GetGetMethod ().IsStatic;
  126. return true;
  127. }
  128. public object Invoke()
  129. {
  130. object[] parsearguments;
  131. if (arguments == null)
  132. parsearguments = new object[0];
  133. else if (HasThis ()) {
  134. parsearguments = new object[arguments.Count - 1];
  135. arguments.CopyTo (parsearguments, 0);
  136. } else {
  137. parsearguments = (arguments as object[]);
  138. if (parsearguments == null) {
  139. parsearguments = new object[arguments.Count];
  140. arguments.CopyTo (parsearguments, 0);
  141. }
  142. }
  143. //MemberInfo member;
  144. switch (member.MemberType) {
  145. case MemberTypes.Constructor:
  146. ConstructorInfo CI = (ConstructorInfo) member;
  147. return CI.Invoke (parsearguments);
  148. case MemberTypes.Method:
  149. MethodInfo MI = (MethodInfo) member;
  150. return MI.Invoke (null, parsearguments);
  151. case MemberTypes.Field:
  152. FieldInfo FI = (FieldInfo) member;
  153. return FI.GetValue (null);
  154. case MemberTypes.Property:
  155. PropertyInfo PI = (PropertyInfo) member;
  156. return PI.GetValue (null, parsearguments);
  157. }
  158. return null;
  159. }
  160. }
  161. }