mscorlib(4.0.0.0) API with additions
InstanceDescriptor.cs
1 using System.Collections;
2 using System.Reflection;
4 
6 {
8  [HostProtection(SecurityAction.LinkDemand, SharedState = true)]
9  [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
10  public sealed class InstanceDescriptor
11  {
12  private MemberInfo member;
13 
14  private ICollection arguments;
15 
16  private bool isComplete;
17 
20  public ICollection Arguments => arguments;
21 
25  public bool IsComplete => isComplete;
26 
29  public MemberInfo MemberInfo => member;
30 
40  public InstanceDescriptor(MemberInfo member, ICollection arguments)
41  : this(member, arguments, isComplete: true)
42  {
43  }
44 
56  public InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete)
57  {
58  this.member = member;
59  this.isComplete = isComplete;
60  if (arguments == null)
61  {
62  this.arguments = new object[0];
63  }
64  else
65  {
66  object[] array = new object[arguments.Count];
67  arguments.CopyTo(array, 0);
68  this.arguments = array;
69  }
70  if (member is FieldInfo)
71  {
72  FieldInfo fieldInfo = (FieldInfo)member;
73  if (!fieldInfo.IsStatic)
74  {
75  throw new ArgumentException(SR.GetString("InstanceDescriptorMustBeStatic"));
76  }
77  if (this.arguments.Count != 0)
78  {
79  throw new ArgumentException(SR.GetString("InstanceDescriptorLengthMismatch"));
80  }
81  }
82  else if (member is ConstructorInfo)
83  {
84  ConstructorInfo constructorInfo = (ConstructorInfo)member;
85  if (constructorInfo.IsStatic)
86  {
87  throw new ArgumentException(SR.GetString("InstanceDescriptorCannotBeStatic"));
88  }
89  if (this.arguments.Count != constructorInfo.GetParameters().Length)
90  {
91  throw new ArgumentException(SR.GetString("InstanceDescriptorLengthMismatch"));
92  }
93  }
94  else if (member is MethodInfo)
95  {
96  MethodInfo methodInfo = (MethodInfo)member;
97  if (!methodInfo.IsStatic)
98  {
99  throw new ArgumentException(SR.GetString("InstanceDescriptorMustBeStatic"));
100  }
101  if (this.arguments.Count != methodInfo.GetParameters().Length)
102  {
103  throw new ArgumentException(SR.GetString("InstanceDescriptorLengthMismatch"));
104  }
105  }
106  else if (member is PropertyInfo)
107  {
108  PropertyInfo propertyInfo = (PropertyInfo)member;
109  if (!propertyInfo.CanRead)
110  {
111  throw new ArgumentException(SR.GetString("InstanceDescriptorMustBeReadable"));
112  }
113  MethodInfo getMethod = propertyInfo.GetGetMethod();
114  if (getMethod != null && !getMethod.IsStatic)
115  {
116  throw new ArgumentException(SR.GetString("InstanceDescriptorMustBeStatic"));
117  }
118  }
119  }
120 
123  public object Invoke()
124  {
125  object[] array = new object[arguments.Count];
126  arguments.CopyTo(array, 0);
127  for (int i = 0; i < array.Length; i++)
128  {
129  if (array[i] is InstanceDescriptor)
130  {
131  array[i] = ((InstanceDescriptor)array[i]).Invoke();
132  }
133  }
134  if (member is ConstructorInfo)
135  {
136  return ((ConstructorInfo)member).Invoke(array);
137  }
138  if (member is MethodInfo)
139  {
140  return ((MethodInfo)member).Invoke(null, array);
141  }
142  if (member is PropertyInfo)
143  {
144  return ((PropertyInfo)member).GetValue(null, array);
145  }
146  if (member is FieldInfo)
147  {
148  return ((FieldInfo)member).GetValue(null);
149  }
150  return null;
151  }
152  }
153 }
Obtains information about the attributes of a member and provides access to member metadata.
Definition: MemberInfo.cs:14
Provides the information necessary to create an instance of an object. This class cannot be inherited...
bool IsStatic
Gets a value indicating whether the field is static.
Definition: FieldInfo.cs:131
abstract MethodInfo GetGetMethod(bool nonPublic)
When overridden in a derived class, returns the public or non-public get accessor for this property.
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
bool IsComplete
Gets a value indicating whether the contents of this T:System.ComponentModel.Design....
Discovers the attributes of a class constructor and provides access to constructor metadata.
Discovers the attributes of a field and provides access to field metadata.
Definition: FieldInfo.cs:15
Definition: __Canon.cs:3
InstanceDescriptor(MemberInfo member, ICollection arguments, bool isComplete)
Initializes a new instance of the T:System.ComponentModel.Design.Serialization.InstanceDescriptor cla...
bool IsStatic
Gets a value indicating whether the method is static.
Definition: MethodBase.cs:227
SecurityAction
Specifies the security actions that can be performed using declarative security.
object Invoke()
Invokes this instance descriptor and returns the object the descriptor describes.
InstanceDescriptor(MemberInfo member, ICollection arguments)
Initializes a new instance of the T:System.ComponentModel.Design.Serialization.InstanceDescriptor cla...
Discovers the attributes of a property and provides access to property metadata.
Definition: PropertyInfo.cs:15
The exception that is thrown when one of the arguments provided to a method is not valid.
ICollection Arguments
Gets the collection of arguments that can be used to reconstruct an instance of the object that this ...
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
abstract bool CanRead
Gets a value indicating whether the property can be read.
Definition: PropertyInfo.cs:44
abstract ParameterInfo [] GetParameters()
When overridden in a derived class, gets the parameters of the specified method or constructor.