Assembly.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.IO;
  5. using System.Globalization;
  6. using System.Collections.Generic;
  7. using System.Configuration.Assemblies;
  8. using System.Runtime.Serialization;
  9. using System.Security;
  10. using System.Runtime.CompilerServices;
  11. namespace System.Reflection
  12. {
  13. public abstract partial class Assembly : ICustomAttributeProvider, ISerializable
  14. {
  15. protected Assembly() { }
  16. public virtual IEnumerable<TypeInfo> DefinedTypes
  17. {
  18. get
  19. {
  20. Type[] types = GetTypes();
  21. TypeInfo[] typeinfos = new TypeInfo[types.Length];
  22. for (int i = 0; i < types.Length; i++)
  23. {
  24. TypeInfo typeinfo = types[i].GetTypeInfo();
  25. if (typeinfo == null)
  26. throw new NotSupportedException(SR.Format(SR.NotSupported_NoTypeInfo, types[i].FullName));
  27. typeinfos[i] = typeinfo;
  28. }
  29. return typeinfos;
  30. }
  31. }
  32. public virtual Type[] GetTypes()
  33. {
  34. Module[] m = GetModules(false);
  35. int finalLength = 0;
  36. Type[][] moduleTypes = new Type[m.Length][];
  37. for (int i = 0; i < moduleTypes.Length; i++)
  38. {
  39. moduleTypes[i] = m[i].GetTypes();
  40. finalLength += moduleTypes[i].Length;
  41. }
  42. int current = 0;
  43. Type[] ret = new Type[finalLength];
  44. for (int i = 0; i < moduleTypes.Length; i++)
  45. {
  46. int length = moduleTypes[i].Length;
  47. Array.Copy(moduleTypes[i], 0, ret, current, length);
  48. current += length;
  49. }
  50. return ret;
  51. }
  52. public virtual IEnumerable<Type> ExportedTypes => GetExportedTypes();
  53. public virtual Type[] GetExportedTypes() { throw NotImplemented.ByDesign; }
  54. public virtual Type[] GetForwardedTypes() { throw NotImplemented.ByDesign; }
  55. public virtual string CodeBase { get { throw NotImplemented.ByDesign; } }
  56. public virtual MethodInfo EntryPoint { get { throw NotImplemented.ByDesign; } }
  57. public virtual string FullName { get { throw NotImplemented.ByDesign; } }
  58. public virtual string ImageRuntimeVersion { get { throw NotImplemented.ByDesign; } }
  59. public virtual bool IsDynamic => false;
  60. public virtual string Location { get { throw NotImplemented.ByDesign; } }
  61. public virtual bool ReflectionOnly { get { throw NotImplemented.ByDesign; } }
  62. public virtual bool IsCollectible => true;
  63. public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName) { throw NotImplemented.ByDesign; }
  64. public virtual string[] GetManifestResourceNames() { throw NotImplemented.ByDesign; }
  65. public virtual Stream GetManifestResourceStream(string name) { throw NotImplemented.ByDesign; }
  66. public virtual Stream GetManifestResourceStream(Type type, string name) { throw NotImplemented.ByDesign; }
  67. public bool IsFullyTrusted => true;
  68. public virtual AssemblyName GetName() => GetName(copiedName: false);
  69. public virtual AssemblyName GetName(bool copiedName) { throw NotImplemented.ByDesign; }
  70. public virtual Type GetType(string name) => GetType(name, throwOnError: false, ignoreCase: false);
  71. public virtual Type GetType(string name, bool throwOnError) => GetType(name, throwOnError: throwOnError, ignoreCase: false);
  72. public virtual Type GetType(string name, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; }
  73. public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }
  74. public virtual IEnumerable<CustomAttributeData> CustomAttributes => GetCustomAttributesData();
  75. public virtual IList<CustomAttributeData> GetCustomAttributesData() { throw NotImplemented.ByDesign; }
  76. public virtual object[] GetCustomAttributes(bool inherit) { throw NotImplemented.ByDesign; }
  77. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }
  78. public virtual string EscapedCodeBase => AssemblyName.EscapeCodeBase(CodeBase);
  79. public object CreateInstance(string typeName) => CreateInstance(typeName, false, BindingFlags.Public | BindingFlags.Instance, binder: null, args: null, culture: null, activationAttributes: null);
  80. public object CreateInstance(string typeName, bool ignoreCase) => CreateInstance(typeName, ignoreCase, BindingFlags.Public | BindingFlags.Instance, binder: null, args: null, culture: null, activationAttributes: null);
  81. public virtual object CreateInstance(string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
  82. {
  83. Type t = GetType(typeName, throwOnError: false, ignoreCase: ignoreCase);
  84. if (t == null)
  85. return null;
  86. return Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes);
  87. }
  88. public virtual event ModuleResolveEventHandler ModuleResolve { add { throw NotImplemented.ByDesign; } remove { throw NotImplemented.ByDesign; } }
  89. public virtual Module ManifestModule { get { throw NotImplemented.ByDesign; } }
  90. public virtual Module GetModule(string name) { throw NotImplemented.ByDesign; }
  91. public Module[] GetModules() => GetModules(getResourceModules: false);
  92. public virtual Module[] GetModules(bool getResourceModules) { throw NotImplemented.ByDesign; }
  93. public virtual IEnumerable<Module> Modules => GetLoadedModules(getResourceModules: true);
  94. public Module[] GetLoadedModules() => GetLoadedModules(getResourceModules: false);
  95. public virtual Module[] GetLoadedModules(bool getResourceModules) { throw NotImplemented.ByDesign; }
  96. public virtual AssemblyName[] GetReferencedAssemblies() { throw NotImplemented.ByDesign; }
  97. public virtual Assembly GetSatelliteAssembly(CultureInfo culture) { throw NotImplemented.ByDesign; }
  98. public virtual Assembly GetSatelliteAssembly(CultureInfo culture, Version version) { throw NotImplemented.ByDesign; }
  99. public virtual FileStream GetFile(string name) { throw NotImplemented.ByDesign; }
  100. public virtual FileStream[] GetFiles() => GetFiles(getResourceModules: false);
  101. public virtual FileStream[] GetFiles(bool getResourceModules) { throw NotImplemented.ByDesign; }
  102. public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; }
  103. public override string ToString()
  104. {
  105. string displayName = FullName;
  106. if (displayName == null)
  107. return base.ToString();
  108. else
  109. return displayName;
  110. }
  111. /*
  112. Returns true if the assembly was loaded from the global assembly cache.
  113. */
  114. public virtual bool GlobalAssemblyCache { get { throw NotImplemented.ByDesign; } }
  115. public virtual long HostContext { get { throw NotImplemented.ByDesign; } }
  116. public override bool Equals(object o) => base.Equals(o);
  117. public override int GetHashCode() => base.GetHashCode();
  118. // Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
  119. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  120. public static bool operator ==(Assembly left, Assembly right)
  121. {
  122. // Test "right" first to allow branch elimination when inlined for null checks (== null)
  123. // so it can become a simple test
  124. if (right is null)
  125. {
  126. // return true/false not the test result https://github.com/dotnet/coreclr/issues/914
  127. return (left is null) ? true : false;
  128. }
  129. // Quick reference equality test prior to calling the virtual Equality
  130. return ReferenceEquals(right, left) ? true : right.Equals(left);
  131. }
  132. public static bool operator !=(Assembly left, Assembly right)
  133. {
  134. return !(left == right);
  135. }
  136. public static string CreateQualifiedName(string assemblyName, string typeName) => typeName + ", " + assemblyName;
  137. public static Assembly GetAssembly(Type type)
  138. {
  139. if (type == null)
  140. throw new ArgumentNullException(nameof(type));
  141. Module m = type.Module;
  142. if (m == null)
  143. return null;
  144. else
  145. return m.Assembly;
  146. }
  147. public static Assembly Load(byte[] rawAssembly) => Load(rawAssembly, rawSymbolStore: null);
  148. [Obsolete("This method has been deprecated. Please use Assembly.Load() instead. https://go.microsoft.com/fwlink/?linkid=14202")]
  149. public static Assembly LoadWithPartialName(string partialName)
  150. {
  151. if (partialName == null)
  152. throw new ArgumentNullException(nameof(partialName));
  153. return Load(partialName);
  154. }
  155. public static Assembly UnsafeLoadFrom(string assemblyFile) => LoadFrom(assemblyFile);
  156. public Module LoadModule(string moduleName, byte[] rawModule) => LoadModule(moduleName, rawModule, null);
  157. public virtual Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore) { throw NotImplemented.ByDesign; }
  158. public static Assembly ReflectionOnlyLoad(byte[] rawAssembly) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
  159. public static Assembly ReflectionOnlyLoad(string assemblyString) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
  160. public static Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
  161. public virtual SecurityRuleSet SecurityRuleSet => SecurityRuleSet.None;
  162. }
  163. }