Merge branch 'bugfix'
[mono.git] / mcs / class / IKVM.Reflection / AssemblyName.cs
1 /*
2   Copyright (C) 2009 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.Globalization;
26 using System.Configuration.Assemblies;
27 using System.IO;
28 using IKVM.Reflection.Reader;
29
30 namespace IKVM.Reflection
31 {
32         public sealed class AssemblyName : ICloneable
33         {
34                 private readonly System.Reflection.AssemblyName name;
35                 internal byte[] hash;
36                 private string culture;
37
38                 private AssemblyName(System.Reflection.AssemblyName name, string culture)
39                 {
40                         this.name = name;
41                         this.culture = culture;
42                 }
43
44                 public AssemblyName()
45                 {
46                         name = new System.Reflection.AssemblyName();
47                 }
48
49                 public AssemblyName(string assemblyName)
50                 {
51                         name = new System.Reflection.AssemblyName(assemblyName);
52                 }
53
54                 public override string ToString()
55                 {
56                         string str = name.ToString();
57                         if (culture != null)
58                         {
59                                 str = str.Replace("Culture=neutral", "Culture=" + culture);
60                         }
61                         return str;
62                 }
63
64                 public string Name
65                 {
66                         get { return name.Name; }
67                         set { name.Name = value; }
68                 }
69
70                 public CultureInfo CultureInfo
71                 {
72                         get { return name.CultureInfo; }
73                         set
74                         {
75                                 name.CultureInfo = value;
76                                 culture = null;
77                         }
78                 }
79
80                 internal string Culture
81                 {
82                         set
83                         {
84                                 culture = value;
85                                 name.CultureInfo = CultureInfo.InvariantCulture;
86                         }
87                 }
88
89                 public Version Version
90                 {
91                         get { return name.Version; }
92                         set { name.Version = value; }
93                 }
94
95                 public StrongNameKeyPair KeyPair
96                 {
97                         get { return name.KeyPair == null ?  null : new StrongNameKeyPair(name.KeyPair); }
98                         set { name.KeyPair = value == null ? null : value.keyPair; }
99                 }
100
101                 public string CodeBase
102                 {
103                         get { return name.CodeBase; }
104                         set { name.CodeBase = value; }
105                 }
106
107                 public ProcessorArchitecture ProcessorArchitecture
108                 {
109                         get { return (ProcessorArchitecture)name.ProcessorArchitecture; }
110                         set { name.ProcessorArchitecture = (System.Reflection.ProcessorArchitecture)value; }
111                 }
112
113                 public AssemblyNameFlags Flags
114                 {
115                         get { return (AssemblyNameFlags)name.Flags; }
116                         set { name.Flags = (System.Reflection.AssemblyNameFlags)value; }
117                 }
118
119                 public AssemblyVersionCompatibility VersionCompatibility
120                 {
121                         get { return name.VersionCompatibility; }
122                         set { name.VersionCompatibility = value; }
123                 }
124
125                 public byte[] GetPublicKey()
126                 {
127                         return name.GetPublicKey();
128                 }
129
130                 public void SetPublicKey(byte[] publicKey)
131                 {
132                         name.SetPublicKey(publicKey);
133                 }
134
135                 public byte[] GetPublicKeyToken()
136                 {
137                         return name.GetPublicKeyToken();
138                 }
139
140                 public void SetPublicKeyToken(byte[] publicKeyToken)
141                 {
142                         name.SetPublicKeyToken(publicKeyToken);
143                 }
144
145                 public AssemblyHashAlgorithm HashAlgorithm
146                 {
147                         get { return name.HashAlgorithm; }
148                         set { name.HashAlgorithm = value; }
149                 }
150
151                 public string FullName
152                 {
153                         get
154                         {
155                                 string str = name.FullName;
156                                 if (culture != null)
157                                 {
158                                         str = str.Replace("Culture=neutral", "Culture=" + culture);
159                                 }
160                                 return str;
161                         }
162                 }
163
164                 public override bool Equals(object obj)
165                 {
166                         AssemblyName other = obj as AssemblyName;
167                         return other != null && other.FullName == this.FullName;
168                 }
169
170                 public override int GetHashCode()
171                 {
172                         return FullName.GetHashCode();
173                 }
174
175                 public object Clone()
176                 {
177                         return new AssemblyName((System.Reflection.AssemblyName)name.Clone(), culture);
178                 }
179
180                 public static bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition)
181                 {
182                         return System.Reflection.AssemblyName.ReferenceMatchesDefinition(reference.name, definition.name);
183                 }
184
185                 public static AssemblyName GetAssemblyName(string path)
186                 {
187                         try
188                         {
189                                 path = Path.GetFullPath(path);
190                                 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
191                                 {
192                                         ModuleReader module = new ModuleReader(null, null, fs, path);
193                                         if (module.Assembly == null)
194                                         {
195                                                 throw new BadImageFormatException("Module does not contain a manifest");
196                                         }
197                                         return module.Assembly.GetName();
198                                 }
199                         }
200                         catch (IOException x)
201                         {
202                                 throw new FileNotFoundException(x.Message, x);
203                         }
204                         catch (UnauthorizedAccessException x)
205                         {
206                                 throw new FileNotFoundException(x.Message, x);
207                         }
208                 }
209         }
210 }