~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/reflect/Assembly.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 delegate Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);
 
30
 
 
31
        public abstract class Assembly : ICustomAttributeProvider
 
32
        {
 
33
                internal readonly Universe universe;
 
34
                protected string fullName;      // AssemblyBuilder needs access to this field to clear it when the name changes
 
35
                protected List<ModuleResolveEventHandler> resolvers;
 
36
 
 
37
                internal Assembly(Universe universe)
 
38
                {
 
39
                        this.universe = universe;
 
40
                }
 
41
 
 
42
                public sealed override string ToString()
 
43
                {
 
44
                        return FullName;
 
45
                }
 
46
 
 
47
                public event ModuleResolveEventHandler ModuleResolve
 
48
                {
 
49
                        add
 
50
                        {
 
51
                                if (resolvers == null)
 
52
                                {
 
53
                                        resolvers = new List<ModuleResolveEventHandler>();
 
54
                                }
 
55
                                resolvers.Add(value);
 
56
                        }
 
57
                        remove
 
58
                        {
 
59
                                resolvers.Remove(value);
 
60
                        }
 
61
                }
 
62
 
 
63
                public abstract Type[] GetTypes();
 
64
                public abstract AssemblyName GetName();
 
65
                public abstract string ImageRuntimeVersion { get; }
 
66
                public abstract Module ManifestModule { get; }
 
67
                public abstract MethodInfo EntryPoint { get; }
 
68
                public abstract string Location { get; }
 
69
                public abstract AssemblyName[] GetReferencedAssemblies();
 
70
                public abstract Module[] GetModules(bool getResourceModules);
 
71
                public abstract Module[] GetLoadedModules(bool getResourceModules);
 
72
                public abstract Module GetModule(string name);
 
73
                public abstract string[] GetManifestResourceNames();
 
74
                public abstract ManifestResourceInfo GetManifestResourceInfo(string resourceName);
 
75
                public abstract System.IO.Stream GetManifestResourceStream(string name);
 
76
 
 
77
                internal abstract Type FindType(TypeName name);
 
78
                internal abstract Type FindTypeIgnoreCase(TypeName lowerCaseName);
 
79
 
 
80
                // The differences between ResolveType and FindType are:
 
81
                // - ResolveType is only used when a type is assumed to exist (because another module's metadata claims it)
 
82
                // - ResolveType can return a MissingType
 
83
                internal Type ResolveType(TypeName typeName)
 
84
                {
 
85
                        return FindType(typeName) ?? universe.GetMissingTypeOrThrow(this.ManifestModule, null, typeName);
 
86
                }
 
87
 
 
88
                public string FullName
 
89
                {
 
90
                        get { return fullName ?? (fullName = GetName().FullName); }
 
91
                }
 
92
 
 
93
                public Module[] GetModules()
 
94
                {
 
95
                        return GetModules(true);
 
96
                }
 
97
 
 
98
                public IEnumerable<Module> Modules
 
99
                {
 
100
                        get { return GetLoadedModules(); }
 
101
                }
 
102
 
 
103
                public Module[] GetLoadedModules()
 
104
                {
 
105
                        return GetLoadedModules(true);
 
106
                }
 
107
 
 
108
                public AssemblyName GetName(bool copiedName)
 
109
                {
 
110
                        return GetName();
 
111
                }
 
112
 
 
113
                public bool ReflectionOnly
 
114
                {
 
115
                        get { return true; }
 
116
                }
 
117
 
 
118
                public Type[] GetExportedTypes()
 
119
                {
 
120
                        List<Type> list = new List<Type>();
 
121
                        foreach (Type type in GetTypes())
 
122
                        {
 
123
                                if (type.IsVisible)
 
124
                                {
 
125
                                        list.Add(type);
 
126
                                }
 
127
                        }
 
128
                        return list.ToArray();
 
129
                }
 
130
 
 
131
                public IEnumerable<Type> ExportedTypes
 
132
                {
 
133
                        get { return GetExportedTypes(); }
 
134
                }
 
135
 
 
136
                public IEnumerable<TypeInfo> DefinedTypes
 
137
                {
 
138
                        get
 
139
                        {
 
140
                                Type[] types = GetTypes();
 
141
                                TypeInfo[] typeInfos = new TypeInfo[types.Length];
 
142
                                for (int i = 0; i < types.Length; i++)
 
143
                                {
 
144
                                        typeInfos[i] = types[i].GetTypeInfo();
 
145
                                }
 
146
                                return typeInfos;
 
147
                        }
 
148
                }
 
149
 
 
150
                public Type GetType(string name)
 
151
                {
 
152
                        return GetType(name, false);
 
153
                }
 
154
 
 
155
                public Type GetType(string name, bool throwOnError)
 
156
                {
 
157
                        return GetType(name, throwOnError, false);
 
158
                }
 
159
 
 
160
                public Type GetType(string name, bool throwOnError, bool ignoreCase)
 
161
                {
 
162
                        TypeNameParser parser = TypeNameParser.Parse(name, throwOnError);
 
163
                        if (parser.Error)
 
164
                        {
 
165
                                return null;
 
166
                        }
 
167
                        if (parser.AssemblyName != null)
 
168
                        {
 
169
                                if (throwOnError)
 
170
                                {
 
171
                                        throw new ArgumentException("Type names passed to Assembly.GetType() must not specify an assembly.");
 
172
                                }
 
173
                                else
 
174
                                {
 
175
                                        return null;
 
176
                                }
 
177
                        }
 
178
                        TypeName typeName = TypeName.Split(TypeNameParser.Unescape(parser.FirstNamePart));
 
179
                        Type type = ignoreCase
 
180
                                ? FindTypeIgnoreCase(typeName.ToLowerInvariant())
 
181
                                : FindType(typeName);
 
182
                        if (type == null && __IsMissing)
 
183
                        {
 
184
                                throw new MissingAssemblyException((MissingAssembly)this);
 
185
                        }
 
186
                        return parser.Expand(type, this, throwOnError, name, false, ignoreCase);
 
187
                }
 
188
 
 
189
                public virtual Module LoadModule(string moduleName, byte[] rawModule)
 
190
                {
 
191
                        throw new NotSupportedException();
 
192
                }
 
193
 
 
194
                public Module LoadModule(string moduleName, byte[] rawModule, byte[] rawSymbolStore)
 
195
                {
 
196
                        return LoadModule(moduleName, rawModule);
 
197
                }
 
198
 
 
199
                public bool IsDefined(Type attributeType, bool inherit)
 
200
                {
 
201
                        return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit).Count != 0;
 
202
                }
 
203
 
 
204
                public IList<CustomAttributeData> __GetCustomAttributes(Type attributeType, bool inherit)
 
205
                {
 
206
                        return CustomAttributeData.__GetCustomAttributes(this, attributeType, inherit);
 
207
                }
 
208
 
 
209
                public IList<CustomAttributeData> GetCustomAttributesData()
 
210
                {
 
211
                        return CustomAttributeData.GetCustomAttributes(this);
 
212
                }
 
213
 
 
214
                public IEnumerable<CustomAttributeData> CustomAttributes
 
215
                {
 
216
                        get { return GetCustomAttributesData(); }
 
217
                }
 
218
 
 
219
                public static string CreateQualifiedName(string assemblyName, string typeName)
 
220
                {
 
221
                        return typeName + ", " + assemblyName;
 
222
                }
 
223
 
 
224
                public static Assembly GetAssembly(Type type)
 
225
                {
 
226
                        return type.Assembly;
 
227
                }
 
228
 
 
229
                public string CodeBase
 
230
                {
 
231
                        get
 
232
                        {
 
233
                                string path = this.Location.Replace(System.IO.Path.DirectorySeparatorChar, '/');
 
234
                                if (!path.StartsWith("/"))
 
235
                                {
 
236
                                        path = "/" + path;
 
237
                                }
 
238
                                return "file://" + path;
 
239
                        }
 
240
                }
 
241
 
 
242
                public virtual bool IsDynamic
 
243
                {
 
244
                        get { return false; }
 
245
                }
 
246
 
 
247
                public virtual bool __IsMissing
 
248
                {
 
249
                        get { return false; }
 
250
                }
 
251
 
 
252
                public AssemblyNameFlags __AssemblyFlags
 
253
                {
 
254
                        get { return GetAssemblyFlags(); }
 
255
                }
 
256
 
 
257
                protected virtual AssemblyNameFlags GetAssemblyFlags()
 
258
                {
 
259
                        return GetName().Flags;
 
260
                }
 
261
 
 
262
                internal abstract IList<CustomAttributeData> GetCustomAttributesData(Type attributeType);
 
263
        }
 
264
}