2 Copyright (C) 2009 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.Globalization;
26 using System.Configuration.Assemblies;
28 using IKVM.Reflection.Reader;
30 namespace IKVM.Reflection
32 public sealed class AssemblyName : ICloneable
34 private readonly System.Reflection.AssemblyName name;
35 private string culture;
37 private AssemblyName(System.Reflection.AssemblyName name, string culture)
40 this.culture = culture;
45 name = new System.Reflection.AssemblyName();
48 public AssemblyName(string assemblyName)
50 name = new System.Reflection.AssemblyName(assemblyName);
53 public override string ToString()
55 string str = name.ToString();
58 str = str.Replace("Culture=neutral", "Culture=" + culture);
65 get { return name.Name; }
66 set { name.Name = value; }
69 public CultureInfo CultureInfo
71 get { return name.CultureInfo; }
74 name.CultureInfo = value;
79 internal string Culture
84 name.CultureInfo = CultureInfo.InvariantCulture;
88 public Version Version
90 get { return name.Version; }
91 set { name.Version = value; }
94 public StrongNameKeyPair KeyPair
96 get { return name.KeyPair == null ? null : new StrongNameKeyPair(name.KeyPair); }
97 set { name.KeyPair = value == null ? null : value.keyPair; }
100 public string CodeBase
102 get { return name.CodeBase; }
103 set { name.CodeBase = value; }
106 public ProcessorArchitecture ProcessorArchitecture
108 get { return (ProcessorArchitecture)name.ProcessorArchitecture; }
109 set { name.ProcessorArchitecture = (System.Reflection.ProcessorArchitecture)value; }
112 public AssemblyNameFlags Flags
114 get { return (AssemblyNameFlags)name.Flags; }
115 set { name.Flags = (System.Reflection.AssemblyNameFlags)value; }
118 public AssemblyVersionCompatibility VersionCompatibility
120 get { return name.VersionCompatibility; }
121 set { name.VersionCompatibility = value; }
124 public byte[] GetPublicKey()
126 return name.GetPublicKey();
129 public void SetPublicKey(byte[] publicKey)
131 name.SetPublicKey(publicKey);
134 public byte[] GetPublicKeyToken()
136 return name.GetPublicKeyToken();
139 public void SetPublicKeyToken(byte[] publicKeyToken)
141 name.SetPublicKeyToken(publicKeyToken);
144 public AssemblyHashAlgorithm HashAlgorithm
146 get { return name.HashAlgorithm; }
147 set { name.HashAlgorithm = value; }
150 public string FullName
154 string str = name.FullName;
157 str = str.Replace("Culture=neutral", "Culture=" + culture);
163 public override bool Equals(object obj)
165 AssemblyName other = obj as AssemblyName;
166 return other != null && other.FullName == this.FullName;
169 public override int GetHashCode()
171 return FullName.GetHashCode();
174 public object Clone()
176 return new AssemblyName((System.Reflection.AssemblyName)name.Clone(), culture);
179 public static bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition)
181 return System.Reflection.AssemblyName.ReferenceMatchesDefinition(reference.name, definition.name);
184 public static AssemblyName GetAssemblyName(string path)
188 path = Path.GetFullPath(path);
189 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
191 ModuleReader module = new ModuleReader(null, null, fs, path);
192 if (module.Assembly == null)
194 throw new BadImageFormatException("Module does not contain a manifest");
196 return module.Assembly.GetName();
199 catch (IOException x)
201 throw new FileNotFoundException(x.Message, x);
203 catch (UnauthorizedAccessException x)
205 throw new FileNotFoundException(x.Message, x);