private static IEnumerable <IAssembly> GetAssemblies(IEnumerable <string> paths, AppDomain domain) { foreach (var path in paths) { yield return(RemoteAssembly.LoadAssembly(path, domain)); } }
public void Load(string path) { // The cache key is the file name plus the full name of the assembly. // This is so we don't load the same assembly more than once from different paths string fileName = Path.GetFileName(path).ToUpperInvariant(); var cacheKey = Tuple.Create(fileName, AssemblyName.GetAssemblyName(path).FullName); Assembly assembly; if (!_assemblyCache.TryGetValue(cacheKey, out assembly)) { // Load the assembly in a reflection only context assembly = Assembly.ReflectionOnlyLoadFrom(path); _assemblyCache[cacheKey] = assembly; } // Get the assembly name and set the properties on this object CopyAssemblyProperties(assembly.GetName(), this); // Do the same for referenced assemblies foreach (AssemblyName referencedAssemblyName in assembly.GetReferencedAssemblies()) { // Copy the properties to the referenced assembly var referencedAssembly = new RemoteAssembly(); _referencedAssemblies.Add(CopyAssemblyProperties(referencedAssemblyName, referencedAssembly)); } }
private static RemoteAssembly CopyAssemblyProperties(AssemblyName assemblyName, RemoteAssembly assembly) { assembly.Name = assemblyName.Name; assembly.Version = assemblyName.Version; assembly.PublicKeyToken = assemblyName.GetPublicKeyTokenString(); string culture = assemblyName.CultureInfo.ToString(); assembly.Culture = String.IsNullOrEmpty(culture) ? "neutral" : culture; return assembly; }
internal static IAssembly LoadAssembly(string path, AppDomain domain) { if (!ReferenceEquals(domain, AppDomain.CurrentDomain)) { RemoteAssembly local1 = domain.CreateInstance <RemoteAssembly>(); local1.Load(path); return(local1); } RemoteAssembly assembly1 = new RemoteAssembly(); assembly1.Load(path); return(assembly1); }
public void RemoteAssemblyComparesById() { // Arrange var assemblyA = new RemoteAssembly("A", null, null, null); var assemblyB = new Mock<IAssembly>(MockBehavior.Strict); assemblyB.SetupGet(b => b.Name).Returns("Z").Verifiable(); // Act var result = RemoteAssembly.Compare(assemblyA, assemblyB.Object); // Assert Assert.Equal(-25, result); assemblyB.Verify(); }
internal static IAssembly LoadAssembly(string path, AppDomain domain) { if (domain != AppDomain.CurrentDomain) { var crossDomainAssembly = domain.CreateInstance <RemoteAssembly>(); crossDomainAssembly.Load(path); return(crossDomainAssembly); } var assembly = new RemoteAssembly(); assembly.Load(path); return(assembly); }
public void RemoteAssemblyComparesByVersionIfIdsAreIdentical() { // Arrange var assemblyA = new RemoteAssembly("A", new Version("2.0.0.0"), null, null); var assemblyB = new Mock<IAssembly>(MockBehavior.Strict); assemblyB.SetupGet(b => b.Name).Returns("A").Verifiable(); assemblyB.SetupGet(b => b.Version).Returns(new Version("1.0.0.0")).Verifiable(); // Act var result = RemoteAssembly.Compare(assemblyA, assemblyB.Object); // Assert Assert.Equal(1, result); assemblyB.Verify(); }
public void RemoteAssemblyComparesByCultureIfIdVersionAndPublicKeyAreIdentical() { // Arrange var assemblyA = new RemoteAssembly("A", new Version("1.0.0.0"), "public-key", "en-us"); var assemblyB = new Mock<IAssembly>(MockBehavior.Strict); assemblyB.SetupGet(b => b.Name).Returns("A").Verifiable(); assemblyB.SetupGet(b => b.Version).Returns(new Version("1.0.0.0")).Verifiable(); assemblyB.SetupGet(b => b.PublicKeyToken).Returns("public-key").Verifiable(); assemblyB.SetupGet(b => b.Culture).Returns("en-uk").Verifiable(); // Act var result = RemoteAssembly.Compare(assemblyA, assemblyB.Object); // Assert Assert.Equal(8, result); assemblyB.Verify(); }
public void Load(string path) { Assembly assembly; Tuple <string, string> key = Tuple.Create <string, string>(Path.GetFileName(path).ToUpperInvariant(), AssemblyName.GetAssemblyName(path).FullName); if (!_assemblyCache.TryGetValue(key, out assembly)) { assembly = Assembly.ReflectionOnlyLoadFrom(path); _assemblyCache[key] = assembly; } CopyAssemblyProperties(assembly.GetName(), this); foreach (AssemblyName name in assembly.GetReferencedAssemblies()) { RemoteAssembly assembly2 = new RemoteAssembly(); this._referencedAssemblies.Add(CopyAssemblyProperties(name, assembly2)); } }
private static RemoteAssembly CopyAssemblyProperties(AssemblyName assemblyName, RemoteAssembly assembly) { assembly.Name = assemblyName.Name; assembly.Version = assemblyName.Version; assembly.PublicKeyToken = assemblyName.GetPublicKeyTokenString(); string culture = assemblyName.CultureInfo.ToString(); assembly.Culture = String.IsNullOrEmpty(culture) ? "neutral" : culture; return(assembly); }
public void RemoteAssemblyReturns0IfAllValuesAreIdentical() { // Arrange var assemblyA = new RemoteAssembly("A", new Version("1.0.0.0"), "public-key", "en-us"); var assemblyB = new RemoteAssembly("A", new Version("1.0.0.0"), "public-key", "en-us"); // Act var result = RemoteAssembly.Compare(assemblyA, assemblyB); // Assert Assert.Equal(0, result); }
public void EqualReturnsFalseIfValuesAreNotIdentical() { // Arrange var assemblyA = new RemoteAssembly("A", new Version("1.0.0.0"), "public-key", "en-us"); var assemblyB = new RemoteAssembly("A", new Version("1.0.0.1"), "public-key", "en-us"); // Act var result = assemblyA.Equals(assemblyB); // Assert Assert.False(result); }
public void RemoteAssemblyReturns1IfValueToBeComparedToIsNull() { // Arrange RemoteAssembly assemblyA = new RemoteAssembly("A", new Version("1.0.0.0"), "public-key", "en-us"); RemoteAssembly assemblyB = null; // Act var result = assemblyA.CompareTo(assemblyB); // Assert Assert.Equal(1, result); }
internal static IAssembly LoadAssembly(string path, AppDomain domain) { if (domain != AppDomain.CurrentDomain) { var crossDomainAssembly = domain.CreateInstance<RemoteAssembly>(); crossDomainAssembly.Load(path); return crossDomainAssembly; } var assembly = new RemoteAssembly(); assembly.Load(path); return assembly; }