Module.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Serialization;
  7. namespace System.Reflection
  8. {
  9. public abstract class Module : ICustomAttributeProvider, ISerializable
  10. {
  11. protected Module() { }
  12. public virtual Assembly Assembly { get { throw NotImplemented.ByDesign; } }
  13. public virtual string FullyQualifiedName { get { throw NotImplemented.ByDesign; } }
  14. public virtual string Name { get { throw NotImplemented.ByDesign; } }
  15. public virtual int MDStreamVersion { get { throw NotImplemented.ByDesign; } }
  16. public virtual Guid ModuleVersionId { get { throw NotImplemented.ByDesign; } }
  17. public virtual string ScopeName { get { throw NotImplemented.ByDesign; } }
  18. public ModuleHandle ModuleHandle => GetModuleHandleImpl();
  19. protected virtual ModuleHandle GetModuleHandleImpl() => ModuleHandle.EmptyHandle; // Not an api but declared protected because of Reflection.Core/Corelib divide (when built by CoreRt)
  20. public virtual void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine) { throw NotImplemented.ByDesign; }
  21. public virtual bool IsResource() { throw NotImplemented.ByDesign; }
  22. public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }
  23. public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData();
  24. public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; }
  25. public virtual object[] GetCustomAttributes(bool inherit) { throw NotImplemented.ByDesign; }
  26. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }
  27. public MethodInfo GetMethod(string name)
  28. {
  29. if (name == null)
  30. throw new ArgumentNullException(nameof(name));
  31. return GetMethodImpl(name, Module.DefaultLookup, null, CallingConventions.Any, null, null);
  32. }
  33. public MethodInfo GetMethod(string name, Type[] types) => GetMethod(name, Module.DefaultLookup, null, CallingConventions.Any, types, null);
  34. public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  35. {
  36. if (name == null)
  37. throw new ArgumentNullException(nameof(name));
  38. if (types == null)
  39. throw new ArgumentNullException(nameof(types));
  40. for (int i = 0; i < types.Length; i++)
  41. {
  42. if (types[i] == null)
  43. throw new ArgumentNullException(nameof(types));
  44. }
  45. return GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
  46. }
  47. protected virtual MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) { throw NotImplemented.ByDesign; }
  48. public MethodInfo[] GetMethods() => GetMethods(Module.DefaultLookup);
  49. public virtual MethodInfo[] GetMethods(BindingFlags bindingFlags) { throw NotImplemented.ByDesign; }
  50. public FieldInfo GetField(string name) => GetField(name, Module.DefaultLookup);
  51. public virtual FieldInfo GetField(string name, BindingFlags bindingAttr) { throw NotImplemented.ByDesign; }
  52. public FieldInfo[] GetFields() => GetFields(Module.DefaultLookup);
  53. public virtual FieldInfo[] GetFields(BindingFlags bindingFlags) { throw NotImplemented.ByDesign; }
  54. public virtual Type[] GetTypes() { throw NotImplemented.ByDesign; }
  55. public virtual Type GetType(string className) => GetType(className, throwOnError: false, ignoreCase: false);
  56. public virtual Type GetType(string className, bool ignoreCase) => GetType(className, throwOnError: false, ignoreCase: ignoreCase);
  57. public virtual Type GetType(string className, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; }
  58. public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
  59. {
  60. Type[] c = GetTypes();
  61. int cnt = 0;
  62. for (int i = 0; i < c.Length; i++)
  63. {
  64. if (filter != null && !filter(c[i], filterCriteria))
  65. c[i] = null;
  66. else
  67. cnt++;
  68. }
  69. if (cnt == c.Length)
  70. return c;
  71. Type[] ret = new Type[cnt];
  72. cnt = 0;
  73. for (int i = 0; i < c.Length; i++)
  74. {
  75. if (c[i] != null)
  76. ret[cnt++] = c[i];
  77. }
  78. return ret;
  79. }
  80. public virtual int MetadataToken { get { throw NotImplemented.ByDesign; } }
  81. public FieldInfo ResolveField(int metadataToken) => ResolveField(metadataToken, null, null);
  82. public virtual FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; }
  83. public MemberInfo ResolveMember(int metadataToken) => ResolveMember(metadataToken, null, null);
  84. public virtual MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; }
  85. public MethodBase ResolveMethod(int metadataToken) => ResolveMethod(metadataToken, null, null);
  86. public virtual MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; }
  87. public virtual byte[] ResolveSignature(int metadataToken) { throw NotImplemented.ByDesign; }
  88. public virtual string ResolveString(int metadataToken) { throw NotImplemented.ByDesign; }
  89. public Type ResolveType(int metadataToken) => ResolveType(metadataToken, null, null);
  90. public virtual Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) { throw NotImplemented.ByDesign; }
  91. public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; }
  92. public override bool Equals(object o) => base.Equals(o);
  93. public override int GetHashCode() => base.GetHashCode();
  94. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  95. public static bool operator ==(Module left, Module right)
  96. {
  97. // Test "right" first to allow branch elimination when inlined for null checks (== null)
  98. // so it can become a simple test
  99. if (right is null)
  100. {
  101. // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
  102. return (left is null) ? true : false;
  103. }
  104. // Try fast reference equality and opposite null check prior to calling the slower virtual Equals
  105. if ((object)left == (object)right)
  106. {
  107. return true;
  108. }
  109. return (left is null) ? false : left.Equals(right);
  110. }
  111. public static bool operator !=(Module left, Module right) => !(left == right);
  112. public override string ToString() => ScopeName;
  113. public static readonly TypeFilter FilterTypeName = (m, c) => FilterTypeNameImpl(m, c, StringComparison.Ordinal);
  114. public static readonly TypeFilter FilterTypeNameIgnoreCase = (m, c) => FilterTypeNameImpl(m, c, StringComparison.OrdinalIgnoreCase);
  115. private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
  116. // FilterTypeName
  117. // This method will filter the class based upon the name. It supports
  118. // a trailing wild card.
  119. private static bool FilterTypeNameImpl(Type cls, object filterCriteria, StringComparison comparison)
  120. {
  121. // Check that the criteria object is a String object
  122. if (!(filterCriteria is string str))
  123. {
  124. throw new InvalidFilterCriteriaException(SR.InvalidFilterCriteriaException_CritString);
  125. }
  126. // Check to see if this is a prefix or exact match requirement
  127. if (str.Length > 0 && str[str.Length - 1] == '*')
  128. {
  129. ReadOnlySpan<char> slice = str.AsSpan(0, str.Length - 1);
  130. return cls.Name.AsSpan().StartsWith(slice, comparison);
  131. }
  132. return cls.Name.Equals(str, comparison);
  133. }
  134. }
  135. }