2 Copyright (C) 2009-2012 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
27 namespace IKVM.Reflection
29 public abstract class Assembly : ICustomAttributeProvider
31 internal readonly Universe universe;
32 protected string fullName; // AssemblyBuilder needs access to this field to clear it when the name changes
34 internal Assembly(Universe universe)
36 this.universe = universe;
39 public sealed override string ToString()
44 public abstract Type[] GetTypes();
45 public abstract AssemblyName GetName();
46 public abstract string ImageRuntimeVersion { get; }
47 public abstract Module ManifestModule { get; }
48 public abstract MethodInfo EntryPoint { get; }
49 public abstract string Location { get; }
50 public abstract AssemblyName[] GetReferencedAssemblies();
51 public abstract Module[] GetModules(bool getResourceModules);
52 public abstract Module[] GetLoadedModules(bool getResourceModules);
53 public abstract Module GetModule(string name);
54 public abstract string[] GetManifestResourceNames();
55 public abstract ManifestResourceInfo GetManifestResourceInfo(string resourceName);
56 public abstract System.IO.Stream GetManifestResourceStream(string name);
58 internal abstract Type FindType(TypeName name);
59 internal abstract Type FindTypeIgnoreCase(TypeName lowerCaseName);
61 // The differences between ResolveType and FindType are:
62 // - ResolveType is only used when a type is assumed to exist (because another module's metadata claims it)
63 // - ResolveType can return a MissingType
64 internal Type ResolveType(TypeName typeName)
66 return FindType(typeName) ?? universe.GetMissingTypeOrThrow(this.ManifestModule, null, typeName);
69 public string FullName
71 get { return fullName ?? (fullName = GetName().FullName); }
74 public Module[] GetModules()
76 return GetModules(true);
79 public Module[] GetLoadedModules()
81 return GetLoadedModules(true);
84 public AssemblyName GetName(bool copiedName)
89 public bool ReflectionOnly
94 public Type[] GetExportedTypes()
96 List<Type> list = new List<Type>();
97 foreach (Type type in GetTypes())
104 return list.ToArray();
107 public Type GetType(string name)
109 return GetType(name, false);
112 public Type GetType(string name, bool throwOnError)
114 return GetType(name, throwOnError, false);
117 public Type GetType(string name, bool throwOnError, bool ignoreCase)
119 TypeNameParser parser = TypeNameParser.Parse(name, throwOnError);
124 if (parser.AssemblyName != null)
128 throw new ArgumentException("Type names passed to Assembly.GetType() must not specify an assembly.");
135 TypeName typeName = TypeName.Split(TypeNameParser.Unescape(parser.FirstNamePart));
136 Type type = ignoreCase
137 ? FindTypeIgnoreCase(typeName.ToLowerInvariant())
138 : FindType(typeName);
139 if (type == null && __IsMissing)
141 throw new MissingAssemblyException((MissingAssembly)this);
143 return parser.Expand(type, this, throwOnError, name, false, ignoreCase);
146 public virtual Module LoadModule(string moduleName, byte[] rawModule)
148 throw new NotSupportedException();
151 public Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore)
153 return LoadModule(moduleName, rawModule);
156 public bool IsDefined(Type attributeType, bool inherit)
158 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
161 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
163 return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
166 public static string CreateQualifiedName(string assemblyName, string typeName)
168 return assemblyName == null ? typeName : typeName + ", " + assemblyName;
171 public static Assembly GetAssembly(Type type)
173 return type.Assembly;
176 public string CodeBase
180 string path = this.Location.Replace(System.IO.Path.DirectorySeparatorChar, '/');
181 if (!path.StartsWith("/"))
185 return "file://" + path;
189 public virtual bool __IsMissing
191 get { return false; }
194 public virtual AssemblyNameFlags __AssemblyFlags
196 get { return GetName().Flags; }
199 internal abstract IList<CustomAttributeData> GetCustomAttributesData(Type attributeType);