public override AssemblyDefinition Resolve (AssemblyNameReference name) { if (name == null) throw new ArgumentNullException ("name"); AssemblyDefinition assembly; if (cache.TryGetValue (name.FullName, out assembly)) return assembly; assembly = base.Resolve (name); cache [name.FullName] = assembly; return assembly; }
AssemblyNameReference ImportScope (SR.Assembly assembly) { AssemblyNameReference scope; #if !SILVERLIGHT var name = assembly.GetName (); if (TryGetAssemblyNameReference (name, out scope)) return scope; scope = new AssemblyNameReference (name.Name, name.Version) { Culture = name.CultureInfo.Name, PublicKeyToken = name.GetPublicKeyToken (), HashAlgorithm = (AssemblyHashAlgorithm) name.HashAlgorithm, }; module.AssemblyReferences.Add (scope); return scope; #else var name = AssemblyNameReference.Parse (assembly.FullName); if (TryGetAssemblyNameReference (name, out scope)) return scope; module.AssemblyReferences.Add (name); return name; #endif }
public static AssemblyNameReference Parse (string fullName) { if (fullName == null) throw new ArgumentNullException ("fullName"); if (fullName.Length == 0) throw new ArgumentException ("Name can not be empty"); var name = new AssemblyNameReference (); var tokens = fullName.Split (','); for (int i = 0; i < tokens.Length; i++) { var token = tokens [i].Trim (); if (i == 0) { name.Name = token; continue; } var parts = token.Split ('='); if (parts.Length != 2) throw new ArgumentException ("Malformed name"); switch (parts [0]) { case "Version": name.Version = new Version (parts [1]); break; case "Culture": name.Culture = parts [1]; break; case "PublicKeyToken": string pk_token = parts [1]; if (pk_token == "null") break; name.PublicKeyToken = new byte [pk_token.Length / 2]; for (int j = 0; j < name.PublicKeyToken.Length; j++) { name.PublicKeyToken [j] = Byte.Parse (pk_token.Substring (j * 2, 2), NumberStyles.HexNumber); } break; } } return name; }
public AssemblyLinkedResource (string name, ManifestResourceAttributes flags, AssemblyNameReference reference) : base (name, flags) { this.reference = reference; }
public AssemblyNameReference GetCorlibReference () { if (corlib != null) return corlib; const string mscorlib = "mscorlib"; var references = module.AssemblyReferences; for (int i = 0; i < references.Count; i++) { var reference = references [i]; if (reference.Name == mscorlib) return corlib = reference; } corlib = new AssemblyNameReference { Name = mscorlib, Version = GetCorlibVersion (), PublicKeyToken = new byte [] { 0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 }, }; references.Add (corlib); return corlib; }
public virtual AssemblyDefinition Resolve (AssemblyNameReference name, ReaderParameters parameters) { if (name == null) throw new ArgumentNullException ("name"); if (parameters == null) parameters = new ReaderParameters (); var assembly = SearchDirectory (name, directories, parameters); if (assembly != null) return assembly; #if !SILVERLIGHT && !CF var framework_dir = Path.GetDirectoryName (typeof (object).Module.FullyQualifiedName); if (IsZero (name.Version)) { assembly = SearchDirectory (name, new [] { framework_dir }, parameters); if (assembly != null) return assembly; } if (name.Name == "mscorlib") { assembly = GetCorlib (name, parameters); if (assembly != null) return assembly; } assembly = GetAssemblyInGac (name, parameters); if (assembly != null) return assembly; assembly = SearchDirectory (name, new [] { framework_dir }, parameters); if (assembly != null) return assembly; #endif if (ResolveFailure != null) { assembly = ResolveFailure (this, name); if (assembly != null) return assembly; } throw new AssemblyResolutionException (name); }
public AssemblyLinkedResource(string name, ManifestResourceAttributes flags, AssemblyNameReference reference) : base(name, flags) { this.reference = reference; }
static string GetAssemblyFile (AssemblyNameReference reference, string prefix, string gac) { var gac_folder = new StringBuilder () .Append (prefix) .Append (reference.Version) .Append ("__"); for (int i = 0; i < reference.PublicKeyToken.Length; i++) gac_folder.Append (reference.PublicKeyToken [i].ToString ("x2")); return Path.Combine ( Path.Combine ( Path.Combine (gac, reference.Name), gac_folder.ToString ()), reference.Name + ".dll"); }
public AssemblyResolveEventArgs (AssemblyNameReference reference) { this.reference = reference; }
AssemblyDefinition GetAssemblyInMonoGac (AssemblyNameReference reference, ReaderParameters parameters) { for (int i = 0; i < gac_paths.Count; i++) { var gac_path = gac_paths [i]; var file = GetAssemblyFile (reference, string.Empty, gac_path); if (File.Exists (file)) return GetAssembly (file, parameters); } return null; }
AssemblyDefinition GetAssemblyInNetGac (AssemblyNameReference reference, ReaderParameters parameters) { var gacs = new [] { "GAC_MSIL", "GAC_32", "GAC" }; var prefixes = new [] { string.Empty, "v4.0_" }; for (int i = 0; i < 2; i++) { for (int j = 0; j < gacs.Length; j++) { var gac = Path.Combine (gac_paths [i], gacs [j]); var file = GetAssemblyFile (reference, prefixes [i], gac); if (Directory.Exists (gac) && File.Exists (file)) return GetAssembly (file, parameters); } } return null; }
AssemblyDefinition GetAssemblyInGac (AssemblyNameReference reference, ReaderParameters parameters) { if (reference.PublicKeyToken == null || reference.PublicKeyToken.Length == 0) return null; if (gac_paths == null) gac_paths = GetGacPaths (); if (on_mono) return GetAssemblyInMonoGac (reference, parameters); return GetAssemblyInNetGac (reference, parameters); }
AssemblyDefinition GetCorlib (AssemblyNameReference reference, ReaderParameters parameters) { var version = reference.Version; var corlib = typeof (object).Assembly.GetName (); if (corlib.Version == version || IsZero (version)) return GetAssembly (typeof (object).Module.FullyQualifiedName, parameters); var path = Directory.GetParent ( Directory.GetParent ( typeof (object).Module.FullyQualifiedName).FullName ).FullName; if (on_mono) { if (version.Major == 1) path = Path.Combine (path, "1.0"); else if (version.Major == 2) { if (version.MajorRevision == 5) path = Path.Combine (path, "2.1"); else path = Path.Combine (path, "2.0"); } else if (version.Major == 4) path = Path.Combine (path, "4.0"); else throw new NotSupportedException ("Version not supported: " + version); } else { switch (version.Major) { case 1: if (version.MajorRevision == 3300) path = Path.Combine (path, "v1.0.3705"); else path = Path.Combine (path, "v1.0.5000.0"); break; case 2: path = Path.Combine (path, "v2.0.50727"); break; case 4: path = Path.Combine (path, "v4.0.30319"); break; default: throw new NotSupportedException ("Version not supported: " + version); } } var file = Path.Combine (path, "mscorlib.dll"); if (File.Exists (file)) return GetAssembly (file, parameters); return null; }
AssemblyDefinition SearchDirectory (AssemblyNameReference name, IEnumerable<string> directories, ReaderParameters parameters) { var extensions = new [] { ".exe", ".dll" }; foreach (var directory in directories) { foreach (var extension in extensions) { string file = Path.Combine (directory, name.Name + extension); if (File.Exists (file)) return GetAssembly (file, parameters); } } return null; }
AssemblyNameReference ImportAssemblyName (AssemblyNameReference name) { AssemblyNameReference reference; if (TryGetAssemblyNameReference (name, out reference)) return reference; reference = new AssemblyNameReference (name.Name, name.Version) { Culture = name.Culture, HashAlgorithm = name.HashAlgorithm, }; var pk_token = !name.PublicKeyToken.IsNullOrEmpty () ? new byte [name.PublicKeyToken.Length] : Empty<byte>.Array; if (pk_token.Length > 0) Buffer.BlockCopy (name.PublicKeyToken, 0, pk_token, 0, pk_token.Length); reference.PublicKeyToken = pk_token; module.AssemblyReferences.Add (reference); return reference; }
public AssemblyResolutionException (AssemblyNameReference reference) : base (string.Format ("Failed to resolve assembly: '{0}'", reference)) { this.reference = reference; }
bool TryGetAssemblyNameReference (AssemblyNameReference name_reference, out AssemblyNameReference assembly_reference) { var references = module.AssemblyReferences; for (int i = 0; i < references.Count; i++) { var reference = references [i]; if (name_reference.FullName != reference.FullName) // TODO compare field by field continue; assembly_reference = reference; return true; } assembly_reference = null; return false; }
public virtual AssemblyDefinition Resolve (AssemblyNameReference name) { return Resolve (name, new ReaderParameters ()); }