Assembly.cs 9.5 KB

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