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