Update mcs/class/System.Core/System/TimeZoneInfo.cs
[mono.git] / mcs / class / IKVM.Reflection / Assembly.cs
1 /*
2   Copyright (C) 2009-2012 Jeroen Frijters
3
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.
7
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:
11
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.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26
27 namespace IKVM.Reflection
28 {
29         public abstract class Assembly : ICustomAttributeProvider
30         {
31                 internal readonly Universe universe;
32                 protected string fullName;      // AssemblyBuilder needs access to this field to clear it when the name changes
33
34                 internal Assembly(Universe universe)
35                 {
36                         this.universe = universe;
37                 }
38
39                 public sealed override string ToString()
40                 {
41                         return FullName;
42                 }
43
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);
57
58                 internal abstract Type FindType(TypeName name);
59                 internal abstract Type FindTypeIgnoreCase(TypeName lowerCaseName);
60
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)
65                 {
66                         return FindType(typeName) ?? universe.GetMissingTypeOrThrow(this.ManifestModule, null, typeName);
67                 }
68
69                 public string FullName
70                 {
71                         get { return fullName ?? (fullName = GetName().FullName); }
72                 }
73
74                 public Module[] GetModules()
75                 {
76                         return GetModules(true);
77                 }
78
79                 public Module[] GetLoadedModules()
80                 {
81                         return GetLoadedModules(true);
82                 }
83
84                 public AssemblyName GetName(bool copiedName)
85                 {
86                         return GetName();
87                 }
88
89                 public bool ReflectionOnly
90                 {
91                         get { return true; }
92                 }
93
94                 public Type[] GetExportedTypes()
95                 {
96                         List<Type> list = new List<Type>();
97                         foreach (Type type in GetTypes())
98                         {
99                                 if (type.IsVisible)
100                                 {
101                                         list.Add(type);
102                                 }
103                         }
104                         return list.ToArray();
105                 }
106
107                 public Type GetType(string name)
108                 {
109                         return GetType(name, false);
110                 }
111
112                 public Type GetType(string name, bool throwOnError)
113                 {
114                         return GetType(name, throwOnError, false);
115                 }
116
117                 public Type GetType(string name, bool throwOnError, bool ignoreCase)
118                 {
119                         TypeNameParser parser = TypeNameParser.Parse(name, throwOnError);
120                         if (parser.Error)
121                         {
122                                 return null;
123                         }
124                         if (parser.AssemblyName != null)
125                         {
126                                 if (throwOnError)
127                                 {
128                                         throw new ArgumentException("Type names passed to Assembly.GetType() must not specify an assembly.");
129                                 }
130                                 else
131                                 {
132                                         return null;
133                                 }
134                         }
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)
140                         {
141                                 throw new MissingAssemblyException((MissingAssembly)this);
142                         }
143                         return parser.Expand(type, this, throwOnError, name, false, ignoreCase);
144                 }
145
146                 public virtual Module LoadModule(string moduleName, byte[] rawModule)
147                 {
148                         throw new NotSupportedException();
149                 }
150
151                 public Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore)
152                 {
153                         return LoadModule(moduleName, rawModule);
154                 }
155
156                 public bool IsDefined(Type attributeType, bool inherit)
157                 {
158                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
159                 }
160
161                 public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
162                 {
163                         return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
164                 }
165
166                 public static string CreateQualifiedName(string assemblyName, string typeName)
167                 {
168                         return typeName + ", " + assemblyName;
169                 }
170
171                 public static Assembly GetAssembly(Type type)
172                 {
173                         return type.Assembly;
174                 }
175
176                 public string CodeBase
177                 {
178                         get
179                         {
180                                 string path = this.Location.Replace(System.IO.Path.DirectorySeparatorChar, '/');
181                                 if (!path.StartsWith("/"))
182                                 {
183                                         path = "/" + path;
184                                 }
185                                 return "file://" + path;
186                         }
187                 }
188
189                 public virtual bool __IsMissing
190                 {
191                         get { return false; }
192                 }
193
194                 public virtual AssemblyNameFlags __AssemblyFlags
195                 {
196                         get { return GetName().Flags; }
197                 }
198
199                 internal abstract IList<CustomAttributeData> GetCustomAttributesData(Type attributeType);
200         }
201 }