How to use AssemblyResolver method of Microsoft.VisualStudio.TestPlatform.Common.Utilities.AssemblyResolver class

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Common.Utilities.AssemblyResolver.AssemblyResolver

AssemblyResolver.cs

Source:AssemblyResolver.cs Github

copy

Full Screen

...10 using System.Reflection;11 using Microsoft.VisualStudio.TestPlatform.ObjectModel;12 using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;13 using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;14 internal class AssemblyResolver : IDisposable15 {16 /// <summary>17 /// The directories to look for assemblies to resolve.18 /// </summary>19 private HashSet<string> searchDirectories;20 /// <summary>21 /// Dictionary of Assemblies discovered to date. Must be locked as it may22 /// be accessed in a multi-threaded context.23 /// </summary>24 private Dictionary<string, Assembly> resolvedAssemblies;25 /// <summary>26 /// Specifies whether the resolver is disposed or not27 /// </summary>28 private bool isDisposed;29 /// <summary>30 /// Assembly resolver for platform31 /// </summary>32 private IAssemblyResolver platformAssemblyResolver;33 private IAssemblyLoadContext platformAssemblyLoadContext;34 private static readonly string[] SupportedFileExtensions = { ".dll", ".exe" };35 /// <summary>36 /// Initializes a new instance of the <see cref="AssemblyResolver"/> class.37 /// </summary>38 /// <param name="directories"> The search directories. </param>39 [System.Security.SecurityCritical]40 public AssemblyResolver(IEnumerable<string> directories)41 {42 this.resolvedAssemblies = new Dictionary<string, Assembly>();43 if (directories == null || !directories.Any())44 {45 this.searchDirectories = new HashSet<string>();46 }47 else48 {49 this.searchDirectories = new HashSet<string>(directories);50 }51 this.platformAssemblyResolver = new PlatformAssemblyResolver();52 this.platformAssemblyLoadContext = new PlatformAssemblyLoadContext();53 this.platformAssemblyResolver.AssemblyResolve += this.OnResolve;54 }55 /// <summary>56 /// Set the directories from which assemblies should be searched57 /// </summary>58 /// <param name="directories"> The search directories. </param>59 [System.Security.SecurityCritical]60 internal void AddSearchDirectories(IEnumerable<string> directories)61 {62 foreach (var directory in directories)63 {64 this.searchDirectories.Add(directory);65 }66 }67 /// <summary>68 /// Assembly Resolve event handler for App Domain - called when CLR loader cannot resolve assembly.69 /// </summary>70 /// <returns>71 /// The <see cref="Assembly"/>.72 /// </returns>73 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]74 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]75 private Assembly OnResolve(object sender, AssemblyResolveEventArgs args)76 {77 if (string.IsNullOrEmpty(args?.Name))78 {79 Debug.Assert(false, "AssemblyResolver.OnResolve: args.Name is null or empty.");80 return null;81 }82 if (this.searchDirectories == null || this.searchDirectories.Count == 0)83 {84 return null;85 }86 EqtTrace.Info("AssemblyResolver: {0}: Resolving assembly.", args.Name);87 // args.Name is like: "Microsoft.VisualStudio.TestTools.Common, Version=[VersionMajor].0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".88 lock (this.resolvedAssemblies)89 {90 if (this.resolvedAssemblies.TryGetValue(args.Name, out var assembly))91 {92 EqtTrace.Info("AssemblyResolver: {0}: Resolved from cache.", args.Name);93 return assembly;94 }95 AssemblyName requestedName = null;96 try97 {98 // Can throw ArgumentException, FileLoadException if arg is empty/wrong format, etc. Should not return null.99 requestedName = new AssemblyName(args.Name);100 }101 catch (Exception ex)102 {103 if (EqtTrace.IsInfoEnabled)104 {105 EqtTrace.Info("AssemblyResolver: {0}: Failed to create assemblyName. Reason:{1} ", args.Name, ex);106 }107 return null;108 }109 Debug.Assert(requestedName != null && !string.IsNullOrEmpty(requestedName.Name), "AssemblyResolver.OnResolve: requested is null or name is empty!");110 foreach (var dir in this.searchDirectories)111 {112 if (string.IsNullOrEmpty(dir))113 {114 continue;115 }116 foreach (var extension in SupportedFileExtensions)117 {118 var assemblyPath = Path.Combine(dir, requestedName.Name + extension);119 try120 {121 if (!File.Exists(assemblyPath))122 {123 continue;124 }125 AssemblyName foundName = this.platformAssemblyLoadContext.GetAssemblyNameFromPath(assemblyPath);126 if (!this.RequestedAssemblyNameMatchesFound(requestedName, foundName))127 {128 continue; // File exists but version/public key is wrong. Try next extension.129 }130 assembly = this.platformAssemblyLoadContext.LoadAssemblyFromPath(assemblyPath);131 this.resolvedAssemblies[args.Name] = assembly;132 EqtTrace.Info("AssemblyResolver: {0}: Resolved assembly. ", args.Name);133 return assembly;134 }135 catch (FileLoadException ex)136 {137 EqtTrace.Info("AssemblyResolver: {0}: Failed to load assembly. Reason:{1} ", args.Name, ex);138 // Rethrow FileLoadException, because this exception means that the assembly139 // was found, but could not be loaded. This will allow us to report a more140 // specific error message to the user for things like access denied.141 throw;142 }143 catch (Exception ex)144 {145 // For all other exceptions, try the next extension.146 EqtTrace.Info("AssemblyResolver: {0}: Failed to load assembly. Reason:{1} ", args.Name, ex);147 }148 }149 }150 }151 return null;152 }153 /// <summary>154 /// Verifies that found assembly name matches requested to avoid security issues.155 /// Looks only at PublicKeyToken and Version, empty matches anything.156 /// VSWhidbey 415774.157 /// </summary>158 /// <returns>159 /// The <see cref="bool"/>.160 /// </returns>161 private bool RequestedAssemblyNameMatchesFound(AssemblyName requestedName, AssemblyName foundName)162 {163 Debug.Assert(requestedName != null);164 Debug.Assert(foundName != null);165 var requestedPublicKey = requestedName.GetPublicKeyToken();166 if (requestedPublicKey != null)167 {168 var foundPublicKey = foundName.GetPublicKeyToken();169 if (foundPublicKey == null)170 {171 return false;172 }173 for (var index = 0; index < requestedPublicKey.Length; ++index)174 {175 if (requestedPublicKey[index] != foundPublicKey[index])176 {177 return false;178 }179 }180 }181 if (requestedName.Version != null)182 {183 return requestedName.Version.Equals(foundName.Version);184 }185 return true;186 }187 ~AssemblyResolver()188 {189 this.Dispose(false);190 }191 public void Dispose()192 {193 this.Dispose(true);194 // Use SupressFinalize in case a subclass195 // of this type implements a finalizer.196 GC.SuppressFinalize(this);197 }198 [System.Security.SecurityCritical]199 protected virtual void Dispose(bool disposing)200 {201 if (!this.isDisposed)202 {203 if (disposing)204 {205 this.platformAssemblyResolver.AssemblyResolve -= this.OnResolve;206 this.platformAssemblyResolver.Dispose();207 }208 this.isDisposed = true;209 }210 }211 }212}...

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7 {8 static void Main(string[] args)9 {10 Microsoft.VisualStudio.TestPlatform.Common.Utilities.AssemblyResolver resolver = new Microsoft.VisualStudio.TestPlatform.Common.Utilities.AssemblyResolver();11 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform");12 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer");13 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Client.dll");14 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Common.dll");15 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Controls.WPF.dll");16 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Controls.dll");17 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Git.Controls.dll");18 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Git.Controls.WPF.dll");19 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Git.Controls.WPF.dll");20 resolver.AddExtensionPath(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Git.Provider.dll");21 resolver.AddExtensionPath(@"C:\

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.IO;7using Microsoft.VisualStudio.TestPlatform.Common.Utilities;8using System.Reflection;9{10 {11 static void Main(string[] args)12 {13 string path = @"C:\Users\user\Documents\Visual Studio 2015\Projects\ConsoleApp1\ConsoleApp1\bin\Debug";14 string assemblyName = "Microsoft.VisualStudio.TestPlatform.ObjectModel.dll";15 AssemblyResolver resolver = new AssemblyResolver();16 resolver.AddResolver(new AssemblyResolver.SimpleResolver(assemblyName, path));17 Assembly assembly = Assembly.Load(assemblyName);18 Console.WriteLine(assembly.FullName);19 Console.ReadLine();20 }21 }22}

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Common.Utilities;2using System;3{4 {5 static void Main(string[] args)6 {7 AssemblyResolver resolver = new AssemblyResolver();8 resolver.AddSearchDirectory(@"C:\Users\Public\Documents\Visual Studio 2017\Projects\ClassLibrary1\ClassLibrary1\bin\Debug");9 resolver.AddSearchDirectory(@"C:\Users\Public\Documents\Visual Studio 2017\Projects\ClassLibrary2\ClassLibrary2\bin\Debug");10 resolver.LoadAssembly("ClassLibrary1");11 resolver.LoadAssembly("ClassLibrary2");12 Console.WriteLine("Hello World!");13 Console.ReadLine();14 }15 }16}

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Common.Utilities;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 AssemblyResolver resolver = new AssemblyResolver();12 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug");13 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin");14 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug");15 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug\bin");16 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug\bin\Debug");17 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug\bin\Debug\bin");18 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug\bin\Debug\bin\Debug");19 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin");20 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug");21 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin");22 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ConsoleApplication1

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Common.Utilities;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 AssemblyResolver resolver = new AssemblyResolver();12 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug");13 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug\bin\Debug");14 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug\bin\Debug\bin\Debug");15 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug\bin\Debug\bin\Debug\bin\Debug");16 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug");17 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug");18 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug");19 resolver.AddResolutionPath(@"C:\Users\Public\Documents\Visual Studio 2013\Projects\ConsoleApp8\ConsoleApp8\bin\Debug\bin\Debug\bin\Debug\bin\Debug\bin\Debug

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1using System;2using Microsoft.VisualStudio.TestPlatform.Common.Utilities;3{4 {5 static void Main(string[] args)6 {7 AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolver.ResolveAssembly;8 var assembly = System.Reflection.Assembly.LoadFrom(@"C:\Users\test\Documents\Visual Studio 2015\Projects\test\test\bin\Debug\test.dll");9 Console.WriteLine("Assembly loaded successfully");10 Console.ReadLine();11 }12 }13}14 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>15 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>16 <ProjectGuid>{D9B9C1A5-5A5E-4A7D-A5C5-7A8D3C3A9E9A}</ProjectGuid>17 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">18 <DefineConstants>DEBUG;TRACE</DefineConstants>19 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Common.Utilities;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 public static void Main()10 {11 AssemblyResolver assemblyResolver = new AssemblyResolver();12 assemblyResolver.AddDependencyLocation(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug");13 assemblyResolver.AddDependencyLocation(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ClassLibrary2\ClassLibrary2\bin\Debug");14 Console.WriteLine("Assembly Resolver");15 Console.ReadKey();16 }17 }18}19using Microsoft.VisualStudio.TestPlatform.Common.Utilities;20using System;21using System.Collections.Generic;22using System.Linq;23using System.Text;24using System.Threading.Tasks;25{26 {27 public static void Main()28 {29 AssemblyResolver assemblyResolver = new AssemblyResolver();30 assemblyResolver.AddDependencyLocation(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug");31 assemblyResolver.AddDependencyLocation(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ClassLibrary2\ClassLibrary2\bin\Debug");32 Console.WriteLine("Assembly Resolver");33 Console.ReadKey();34 }35 }36}37using Microsoft.VisualStudio.TestPlatform.Common.Utilities;38using System;39using System.Collections.Generic;40using System.Linq;41using System.Text;42using System.Threading.Tasks;43{44 {45 public static void Main()46 {47 AssemblyResolver assemblyResolver = new AssemblyResolver();48 assemblyResolver.AddDependencyLocation(@"C:\Users\Public\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug");

Full Screen

Full Screen

AssemblyResolver

Using AI Code Generation

copy

Full Screen

1{2 public void AssemblyResolver()3 {4 var assemblyResolver = new AssemblyResolver();5 var assembly = assemblyResolver.ResolveAssemblyFromPath(@"C:\Users\test\Documents\Visual Studio 2015\Projects\test\test\bin\Debug\test.exe");6 Assert.IsNotNull(assembly);7 }8}9{10 public void AssemblyResolver()11 {12 var assemblyResolver = new AssemblyResolver();13 var assembly = assemblyResolver.ResolveAssemblyFromPath(@"C:\Users\test\Documents\Visual Studio 2015\Projects\test\test\bin\Debug\test.dll");14 Assert.IsNotNull(assembly);15 }16}17{18 public void AssemblyResolver()19 {20 var assemblyResolver = new AssemblyResolver();21 var assembly = assemblyResolver.ResolveAssemblyFromPath(@"C:\Users\test\Documents\Visual Studio 2015\Projects\test\test\bin\Debug\test.dll");22 Assert.IsNotNull(assembly);23 }24}25{26 public void AssemblyResolver()27 {28 var assemblyResolver = new AssemblyResolver();29 var assembly = assemblyResolver.ResolveAssemblyFromPath(@"C:\Users\test\Documents\Visual Studio 2015\Projects\test\test\bin\Debug\test.dll");30 Assert.IsNotNull(assembly);31 }32}33{34 public void AssemblyResolver()35 {36 var assemblyResolver = new AssemblyResolver();37 var assembly = assemblyResolver.ResolveAssemblyFromPath(@"C:\Users\test\Documents\Visual Studio 2015\Projects\test\test\bin\Debug\test.dll");38 Assert.IsNotNull(assembly);39 }40}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Vstest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful