Module.cs 8.1 KB

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