AppDomainToolkit.AssemblyLoader.LoadAssemblyWithReferences C# (CSharp) Method

LoadAssemblyWithReferences() public method

This implementation will perform a best-effort load of the target assembly and it's required references into the current application domain. The .NET framework pins us in on which call we're allowed to use when loading these assemblies, so we'll need to rely on the AssemblyResolver instance attached to the AppDomain in order to load the way we want.
public LoadAssemblyWithReferences ( LoadMethod loadMethod, string assemblyPath ) : IList
loadMethod LoadMethod
assemblyPath string
return IList
        public IList<Assembly> LoadAssemblyWithReferences(LoadMethod loadMethod, string assemblyPath)
        {
            var list = new List<Assembly>();
            var assembly = this.LoadAssembly(loadMethod, assemblyPath);
            list.Add(assembly);

            foreach (var reference in assembly.GetReferencedAssemblies())
            {
                list.Add(Assembly.Load(reference));
            }

            return list;
        }

Usage Example

Example #1
0
        /// <inheritdoc />
        public ITestRunResult RunTests(IAssemblyTarget target, IAssemblyTarget tests)
        {
            ITestRunResult result;

            try
            {
                // Load tests and target
                var loader = new AssemblyLoader();
                loader.LoadAssemblyWithReferences(LoadMethod.LoadFrom, target.Location);
                var testAssembly = loader.LoadAssembly(LoadMethod.LoadFrom, tests.Location);

                // Grab the reflection wrapper.
                var testUtilReflectionWrapper = new TestUtilReflectionWrapper(loader);

                // If we have ignore targets, set up the appropriate test state recorder.
                if (this.ignoreTargets != null && this.ignoreTargets.Any())
                {
                    testUtilReflectionWrapper.InvokeStateRecorderPropertySet(this.BuildTestStateRecorder());
                }

                // Execute the tests.
                foreach (var type in testAssembly.GetTypes())
                {
                    var testObj = Activator.CreateInstance(type);
                    var method = type.GetMethods().First(x => x.Name.Equals(this.testMethodName));
                    method.Invoke(testObj, null);
                }

                // Execute Save by finding the loaded winbert assembly and calling TestUtil.SaveResults()
                var analysisLogPath = CreateAnalysisFilePath(target, tests);
                testUtilReflectionWrapper.InvokeSaveResults(analysisLogPath);
                result = TestRunResult.Successful(analysisLogPath, target);
            }
            catch (Exception)
            {
                result = TestRunResult.Failure(target);
            }

            return result;
        }