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;
36 private string culture;
38 private AssemblyName(System.Reflection.AssemblyName name, string culture)
41 this.culture = culture;
46 name = new System.Reflection.AssemblyName();
49 public AssemblyName(string assemblyName)
51 name = new System.Reflection.AssemblyName(assemblyName);
54 public override string ToString()
56 string str = name.ToString();
59 str = str.Replace("Culture=neutral", "Culture=" + culture);
66 get { return name.Name; }
67 set { name.Name = value; }
70 public CultureInfo CultureInfo
72 get { return name.CultureInfo; }
75 name.CultureInfo = value;
80 internal string Culture
85 name.CultureInfo = CultureInfo.InvariantCulture;
89 public Version Version
91 get { return name.Version; }
92 set { name.Version = value; }
95 public StrongNameKeyPair KeyPair
97 get { return name.KeyPair == null ? null : new StrongNameKeyPair(name.KeyPair); }
98 set { name.KeyPair = value == null ? null : value.keyPair; }
101 public string CodeBase
103 get { return name.CodeBase; }
104 set { name.CodeBase = value; }
107 public ProcessorArchitecture ProcessorArchitecture
109 get { return (ProcessorArchitecture)name.ProcessorArchitecture; }
110 set { name.ProcessorArchitecture = (System.Reflection.ProcessorArchitecture)value; }
113 public AssemblyNameFlags Flags
115 get { return (AssemblyNameFlags)name.Flags; }
116 set { name.Flags = (System.Reflection.AssemblyNameFlags)value; }
119 public AssemblyVersionCompatibility VersionCompatibility
121 get { return name.VersionCompatibility; }
122 set { name.VersionCompatibility = value; }
125 public byte[] GetPublicKey()
127 return name.GetPublicKey();
130 public void SetPublicKey(byte[] publicKey)
132 name.SetPublicKey(publicKey);
135 public byte[] GetPublicKeyToken()
137 return name.GetPublicKeyToken();
140 public void SetPublicKeyToken(byte[] publicKeyToken)
142 name.SetPublicKeyToken(publicKeyToken);
145 public AssemblyHashAlgorithm HashAlgorithm
147 get { return name.HashAlgorithm; }
148 set { name.HashAlgorithm = value; }
151 public string FullName
155 string str = name.FullName;
158 str = str.Replace("Culture=neutral", "Culture=" + culture);
164 public override bool Equals(object obj)
166 AssemblyName other = obj as AssemblyName;
167 return other != null && other.FullName == this.FullName;
170 public override int GetHashCode()
172 return FullName.GetHashCode();
175 public object Clone()
177 return new AssemblyName((System.Reflection.AssemblyName)name.Clone(), culture);
180 public static bool ReferenceMatchesDefinition(AssemblyName reference, AssemblyName definition)
182 return System.Reflection.AssemblyName.ReferenceMatchesDefinition(reference.name, definition.name);
185 public static AssemblyName GetAssemblyName(string path)
189 path = Path.GetFullPath(path);
190 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
192 ModuleReader module = new ModuleReader(null, null, fs, path);
193 if (module.Assembly == null)
195 throw new BadImageFormatException("Module does not contain a manifest");
197 return module.Assembly.GetName();
200 catch (IOException x)
202 throw new FileNotFoundException(x.Message, x);
204 catch (UnauthorizedAccessException x)
206 throw new FileNotFoundException(x.Message, x);