1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 // NOTE: These are extension methods in the contract, but plain static methods
6 // in this implementation. This is done to avoid confusion around what would
7 // look like infinite recursion in the implementation. Callers compiled against
8 // the contract will still be able to invoke them as extension methods and get
9 // source compatibility with classic reflection code.
11 // However, this does not apply if there is no 1:1 correspondence with an instance
12 // in mscorlib. New extension methods should be marked with 'this'.
14 namespace System.Reflection
16 public static class TypeExtensions
18 public static ConstructorInfo GetConstructor(Type type, Type[] types)
20 Requires.NotNull(type, nameof(type));
21 return type.GetConstructor(types);
24 public static ConstructorInfo[] GetConstructors(Type type)
26 Requires.NotNull(type, nameof(type));
27 return type.GetConstructors();
30 public static ConstructorInfo[] GetConstructors(Type type, BindingFlags bindingAttr)
32 Requires.NotNull(type, nameof(type));
33 return type.GetConstructors(bindingAttr);
36 public static MemberInfo[] GetDefaultMembers(Type type)
38 Requires.NotNull(type, nameof(type));
39 return type.GetDefaultMembers();
42 public static EventInfo GetEvent(Type type, string name)
44 Requires.NotNull(type, nameof(type));
45 return type.GetEvent(name);
48 public static EventInfo GetEvent(Type type, string name, BindingFlags bindingAttr)
50 Requires.NotNull(type, nameof(type));
51 return type.GetEvent(name, bindingAttr);
54 public static EventInfo[] GetEvents(Type type)
56 Requires.NotNull(type, nameof(type));
57 return type.GetEvents();
60 public static EventInfo[] GetEvents(Type type, BindingFlags bindingAttr)
62 Requires.NotNull(type, nameof(type));
63 return type.GetEvents(bindingAttr);
66 public static FieldInfo GetField(Type type, string name)
68 Requires.NotNull(type, nameof(type));
69 return type.GetField(name);
72 public static FieldInfo GetField(Type type, string name, BindingFlags bindingAttr)
74 Requires.NotNull(type, nameof(type));
75 return type.GetField(name, bindingAttr);
78 public static FieldInfo[] GetFields(Type type)
80 Requires.NotNull(type, nameof(type));
81 return type.GetFields();
84 public static FieldInfo[] GetFields(Type type, BindingFlags bindingAttr)
86 Requires.NotNull(type, nameof(type));
87 return type.GetFields(bindingAttr);
90 public static Type[] GetGenericArguments(Type type)
92 Requires.NotNull(type, nameof(type));
93 return type.GetGenericArguments();
96 public static Type[] GetInterfaces(Type type)
98 Requires.NotNull(type, nameof(type));
99 return type.GetInterfaces();
102 public static MemberInfo[] GetMember(Type type, string name)
104 Requires.NotNull(type, nameof(type));
105 return type.GetMember(name);
108 public static MemberInfo[] GetMember(Type type, string name, BindingFlags bindingAttr)
110 Requires.NotNull(type, nameof(type));
111 return type.GetMember(name, bindingAttr);
114 public static MemberInfo[] GetMembers(Type type)
116 Requires.NotNull(type, nameof(type));
117 return type.GetMembers();
120 public static MemberInfo[] GetMembers(Type type, BindingFlags bindingAttr)
122 Requires.NotNull(type, nameof(type));
123 return type.GetMembers(bindingAttr);
126 public static MethodInfo GetMethod(Type type, string name)
128 Requires.NotNull(type, nameof(type));
129 return type.GetMethod(name);
132 public static MethodInfo GetMethod(Type type, string name, BindingFlags bindingAttr)
134 Requires.NotNull(type, nameof(type));
135 return type.GetMethod(name, bindingAttr);
138 public static MethodInfo GetMethod(Type type, string name, Type[] types)
140 Requires.NotNull(type, nameof(type));
141 return type.GetMethod(name, types);
144 public static MethodInfo[] GetMethods(Type type)
146 Requires.NotNull(type, nameof(type));
147 return type.GetMethods();
150 public static MethodInfo[] GetMethods(Type type, BindingFlags bindingAttr)
152 Requires.NotNull(type, nameof(type));
153 return type.GetMethods(bindingAttr);
156 public static Type GetNestedType(Type type, string name, BindingFlags bindingAttr)
158 Requires.NotNull(type, nameof(type));
159 return type.GetNestedType(name, bindingAttr);
162 public static Type[] GetNestedTypes(Type type, BindingFlags bindingAttr)
164 Requires.NotNull(type, nameof(type));
165 return type.GetNestedTypes(bindingAttr);
168 public static PropertyInfo[] GetProperties(Type type)
170 Requires.NotNull(type, nameof(type));
171 return type.GetProperties();
174 public static PropertyInfo[] GetProperties(Type type, BindingFlags bindingAttr)
176 Requires.NotNull(type, nameof(type));
177 return type.GetProperties(bindingAttr);
180 public static PropertyInfo GetProperty(Type type, string name)
182 Requires.NotNull(type, nameof(type));
183 return type.GetProperty(name);
186 public static PropertyInfo GetProperty(Type type, string name, BindingFlags bindingAttr)
188 Requires.NotNull(type, nameof(type));
189 return type.GetProperty(name, bindingAttr);
192 public static PropertyInfo GetProperty(Type type, string name, Type returnType)
194 Requires.NotNull(type, nameof(type));
195 return type.GetProperty(name, returnType);
198 public static PropertyInfo GetProperty(Type type, string name, Type returnType, Type[] types)
200 Requires.NotNull(type, nameof(type));
201 return type.GetProperty(name, returnType, types);
204 public static bool IsAssignableFrom(Type type, Type c)
206 Requires.NotNull(type, nameof(type));
207 return type.IsAssignableFrom(c);
210 public static bool IsInstanceOfType(Type type, object o)
212 Requires.NotNull(type, nameof(type));
213 return type.IsInstanceOfType(o);
217 public static class AssemblyExtensions
219 public static Type[] GetExportedTypes(Assembly assembly)
221 Requires.NotNull(assembly, nameof(assembly));
222 return assembly.GetExportedTypes();
225 public static Module[] GetModules(Assembly assembly)
227 Requires.NotNull(assembly, nameof(assembly));
228 return assembly.GetModules();
231 public static Type[] GetTypes(Assembly assembly)
233 Requires.NotNull(assembly, nameof(assembly));
234 return assembly.GetTypes();
238 public static class EventInfoExtensions
240 public static MethodInfo GetAddMethod(EventInfo eventInfo)
242 Requires.NotNull(eventInfo, nameof(eventInfo));
243 return eventInfo.GetAddMethod();
246 public static MethodInfo GetAddMethod(EventInfo eventInfo, bool nonPublic)
248 Requires.NotNull(eventInfo, nameof(eventInfo));
249 return eventInfo.GetAddMethod(nonPublic);
252 public static MethodInfo GetRaiseMethod(EventInfo eventInfo)
254 Requires.NotNull(eventInfo, nameof(eventInfo));
255 return eventInfo.GetRaiseMethod();
258 public static MethodInfo GetRaiseMethod(EventInfo eventInfo, bool nonPublic)
260 Requires.NotNull(eventInfo, nameof(eventInfo));
261 return eventInfo.GetRaiseMethod(nonPublic);
264 public static MethodInfo GetRemoveMethod(EventInfo eventInfo)
266 Requires.NotNull(eventInfo, nameof(eventInfo));
267 return eventInfo.GetRemoveMethod();
270 public static MethodInfo GetRemoveMethod(EventInfo eventInfo, bool nonPublic)
272 Requires.NotNull(eventInfo, nameof(eventInfo));
273 return eventInfo.GetRemoveMethod(nonPublic);
277 public static class MemberInfoExtensions
281 /// Determines if there is a metadata token available for the given member.
282 /// <see cref="GetMetadataToken(MemberInfo)"/> throws <see cref="InvalidOperationException"/> otherwise.
284 /// <remarks>This maybe</remarks>
285 public static bool HasMetadataToken(this MemberInfo member)
287 Requires.NotNull(member, nameof(member));
291 return GetMetadataTokenOrZeroOrThrow(member) != 0;
293 catch (InvalidOperationException)
295 // Thrown for unbaked ref-emit members/types.
296 // Other cases such as typeof(byte[]).MetadataToken will be handled by comparison to zero above.
302 /// Gets a metadata token for the given member if available. The returned token is never nil.
304 /// <exception cref="InvalidOperationException">
305 /// There is no metadata token available. <see cref="HasMetadataToken(MemberInfo)"/> returns false in this case.
307 public static int GetMetadataToken(this MemberInfo member)
309 Requires.NotNull(member, nameof(member));
311 int token = GetMetadataTokenOrZeroOrThrow(member);
315 throw new InvalidOperationException(SR.NoMetadataTokenAvailable);
321 private static int GetMetadataTokenOrZeroOrThrow(MemberInfo member)
323 int token = member.MetadataToken;
325 // Tokens have MSB = table index, 3 LSBs = row index
326 // row index of 0 is a nil token
327 const int rowMask = 0x00FFFFFF;
328 if ((token & rowMask) == 0)
330 // Nil token is returned for edge cases like typeof(byte[]).MetadataToken.
338 public static class MethodInfoExtensions
340 public static MethodInfo GetBaseDefinition(MethodInfo method)
342 Requires.NotNull(method, nameof(method));
343 return method.GetBaseDefinition();
347 public static class ModuleExtensions
349 public static bool HasModuleVersionId(this Module module)
351 Requires.NotNull(module, nameof(module));
352 return true; // not expected to fail on platforms with Module.ModuleVersionId built-in.
355 public static Guid GetModuleVersionId(this Module module)
357 Requires.NotNull(module, nameof(module));
358 return module.ModuleVersionId;
362 public static class PropertyInfoExtensions
364 public static MethodInfo[] GetAccessors(PropertyInfo property)
366 Requires.NotNull(property, nameof(property));
367 return property.GetAccessors();
370 public static MethodInfo[] GetAccessors(PropertyInfo property, bool nonPublic)
372 Requires.NotNull(property, nameof(property));
373 return property.GetAccessors(nonPublic);
376 public static MethodInfo GetGetMethod(PropertyInfo property)
378 Requires.NotNull(property, nameof(property));
379 return property.GetGetMethod();
382 public static MethodInfo GetGetMethod(PropertyInfo property, bool nonPublic)
384 Requires.NotNull(property, nameof(property));
385 return property.GetGetMethod(nonPublic);
388 public static MethodInfo GetSetMethod(PropertyInfo property)
390 Requires.NotNull(property, nameof(property));
391 return property.GetSetMethod();
394 public static MethodInfo GetSetMethod(PropertyInfo property, bool nonPublic)
396 Requires.NotNull(property, nameof(property));
397 return property.GetSetMethod(nonPublic);