AppDomainToolkit.AssemblyLoader.LoadAssembly C# (CSharp) Метод

LoadAssembly() публичный Метод

If the LoadMethod for this instance is set to LoadBits and the path to the PDB file is unspecified then we will attempt to guess the path to the PDB and load it. Note that if an assembly is loaded into memory without it's debugging symbols then an image exception will be thrown. Be wary of this. Loading an Assembly with the LoadBits method will not lock the DLL file because the entire assembly is loaded into memory and the file handle is closed. Note that, however, Assemblies loaded in this manner will not have a location associated with them--so you must then key the Assembly by it's strong name. This can cause problems when loading multiple versions of the same assembly into a single application domain.
public LoadAssembly ( LoadMethod loadMethod, string assemblyPath, string pdbPath = null ) : Assembly
loadMethod LoadMethod
assemblyPath string
pdbPath string
Результат System.Reflection.Assembly
        public Assembly LoadAssembly(LoadMethod loadMethod, string assemblyPath, string pdbPath = null)
        {
            Assembly assembly = null;
            switch (loadMethod)
            {
                case LoadMethod.LoadFrom:
                    assembly = Assembly.LoadFrom(assemblyPath);
                    break;
                case LoadMethod.LoadFile:
                    assembly = Assembly.LoadFile(assemblyPath);
                    break;
                case LoadMethod.LoadBits:

                    // Attempt to load the PDB bits along with the assembly to avoid image exceptions.
                    pdbPath = string.IsNullOrEmpty(pdbPath) ? Path.ChangeExtension(assemblyPath, "pdb") : pdbPath;

                    // Only load the PDB if it exists--we may be dealing with a release assembly.
                    if (File.Exists(pdbPath))
                    {
                        assembly = Assembly.Load(
                            File.ReadAllBytes(assemblyPath),
                            File.ReadAllBytes(pdbPath));
                    }
                    else
                    {
                        assembly = Assembly.Load(File.ReadAllBytes(assemblyPath));
                    }

                    break;
                default:
                    // In case we upadate the enum but forget to update this logic.
                    throw new NotSupportedException("The target load method isn't supported!");
            }

            return assembly;
        }

Usage 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;
        }