2
0

InstanceDescriptor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. //
  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. namespace System.ComponentModel.Design.Serialization
  34. {
  35. public sealed class InstanceDescriptor
  36. {
  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)
  48. throw new ArgumentNullException ("member", "MemberInfo must be valid");
  49. if (!IsMemberValid (member, arguments))
  50. throw new ArgumentException ("Only Constructor, Method, Field or Property members allowed", "member");
  51. this.member = member;
  52. this.arguments = arguments;
  53. }
  54. private bool IsMemberValid (MemberInfo member, ICollection arguments)
  55. {
  56. switch (member.MemberType) {
  57. // According to docs only these types are allowed
  58. case MemberTypes.Constructor:
  59. ConstructorInfo CI = (ConstructorInfo) member;
  60. if (!CI.IsStatic)
  61. throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
  62. if (arguments == null) // null counts as no arguments
  63. if (CI.GetParameters().Length != 0)
  64. throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
  65. if (arguments.Count != CI.GetParameters().Length)
  66. throw new ArgumentException ("Invalid number of arguments for this constructor", "arguments");
  67. return true;
  68. case MemberTypes.Method:
  69. MethodInfo MI = (MethodInfo) member;
  70. if (!MI.IsStatic)
  71. throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
  72. if (arguments == null) // null counts as no arguments
  73. if (MI.GetParameters().Length != 0)
  74. throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
  75. if (arguments.Count != MI.GetParameters().Length)
  76. throw new ArgumentException ("Invalid number of arguments for this method", "arguments");
  77. return true;
  78. case MemberTypes.Field:
  79. FieldInfo FI = (FieldInfo) member;
  80. if (!FI.IsStatic)
  81. throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
  82. if (arguments == null) // null counts as no arguments
  83. return true;
  84. if (arguments.Count == 0)
  85. throw new ArgumentException ("Field members do not take any arguments", "arguments");
  86. return true;
  87. case MemberTypes.Property:
  88. PropertyInfo PI = (PropertyInfo) member;
  89. if (!(PI.CanRead))
  90. throw new ArgumentException ("That property cannot be read", "member");
  91. MethodInfo PIM = PI.GetGetMethod();
  92. if (!PIM.IsStatic)
  93. throw new ArgumentException ("InstanceDescriptor only describes static (VB.Net: shared) members", "member");
  94. if (arguments == null) // null counts as no arguments
  95. if (PIM.GetParameters().Length != 0)
  96. throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
  97. if (arguments.Count != PIM.GetParameters().Length)
  98. throw new ArgumentException ("Invalid number of arguments for this property", "arguments");
  99. return true;
  100. }
  101. return false;
  102. }
  103. public ICollection Arguments {
  104. get {
  105. // It seems MS does not return null even if we specified null as parameter (but does not cause an exception)
  106. if (arguments == null)
  107. return new object[0];
  108. return arguments;
  109. }
  110. }
  111. public bool IsComplete {
  112. get { return isComplete; }
  113. }
  114. public MemberInfo MemberInfo {
  115. get { return member; }
  116. }
  117. public object Invoke()
  118. {
  119. object[] parsearguments;
  120. if (arguments == null)
  121. parsearguments = new object[0];
  122. else {
  123. parsearguments = new object[arguments.Count - 1];
  124. arguments.CopyTo (parsearguments, 0);
  125. }
  126. //MemberInfo member;
  127. switch (member.MemberType) {
  128. case MemberTypes.Constructor:
  129. ConstructorInfo CI = (ConstructorInfo) member;
  130. return CI.Invoke (parsearguments);
  131. case MemberTypes.Method:
  132. MethodInfo MI = (MethodInfo) member;
  133. return MI.Invoke (null, parsearguments);
  134. case MemberTypes.Field:
  135. FieldInfo FI = (FieldInfo) member;
  136. return FI.GetValue (null);
  137. case MemberTypes.Property:
  138. PropertyInfo PI = (PropertyInfo) member;
  139. return PI.GetValue (null, parsearguments);
  140. }
  141. return null;
  142. }
  143. }
  144. }