Пример #1
0
        public static IEnumerable <IWrapper> LoadDynamicWrapper(string assemblyPath)
        {
            lock (_loadDynamicWrapperLockObj)
            {
                if (!_dynamicLoadAssemblyStatus.TryGetValue(assemblyPath.ToLower(), out var wrappers))
                {
                    var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <IWrapper>(assemblyPath);

                    foreach (var ex in result.Exceptions)
                    {
                        Log.Warn($"An exception occurred while loading an extension from assembly {assemblyPath}: {ex}");
                    }

                    wrappers = result.Instances.ToArray();

                    _dynamicLoadAssemblyStatus[assemblyPath.ToLower()] = wrappers;

                    if (Log.IsFinestEnabled)
                    {
                        Log.Finest($"Dynamically loaded wrappers from assembly {assemblyPath}: {string.Join(", ", (IEnumerable<IWrapper>)wrappers)}");
                    }
                }

                return(wrappers);
            }
        }
        public void when_null_directory()
        {
            var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <IInterface>((string)null);

            Assert.AreEqual(0, result.Instances.Count());
            Assert.AreEqual(0, result.Exceptions.Count());
        }
        public void when_invalid_path()
        {
            var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <IInterface>("!@#$%");

            Assert.AreEqual(0, result.Instances.Count());
            Assert.AreEqual(0, result.Exceptions.Count());
        }
        public void when_non_existant_directory_on_disk()
        {
            var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <IInterface>("C:\\Foo\\Bar.dll");

            Assert.AreEqual(0, result.Instances.Count());
            Assert.AreEqual(0, result.Exceptions.Count());
        }
        public void when_type_load_exception()
        {
            var directory = MakeTempDirForTest();

            Action <string> testMethod1 = directoryPath =>
            {
                var interfaceAssemblyPath = Path.Combine(directoryPath, "IDoSomething.dll");
                var interfaceAssembly     = GenerateAssembly(@"public interface IDoSomething : IInterface { }", interfaceAssemblyPath);

                var concreteAssemblyPath = Path.Combine(directoryPath, "DoNothing.dll");
                GenerateAssembly(@"public class DoNothing : IDoSomething { }", concreteAssemblyPath, additionalAssemblyReferences: new[] { interfaceAssembly });
            };

            AppDomainExtensions.IsolateMethodInAppDomain(testMethod1, directory.FullName);

            Action <string> testMethod2 = directoryPath =>
            {
                // Add a method to IDoSomething so that our DoNothing class is no longer a valid implementation and will throw a TypeLoadException when loaded
                var interfaceAssemblyPath = Path.Combine(directoryPath, "IDoSomething.dll");
                GenerateAssembly(@"public interface IDoSomething : IInterface { void DoSomething(); }", interfaceAssemblyPath);

                var files = Directory.GetFiles(directoryPath);

                var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <IInterface>(files);
                Assert.AreEqual(0, result.Instances.Count());
                Assert.AreEqual(1, result.Exceptions.Count());
            };

            AppDomainExtensions.IsolateMethodInAppDomain(testMethod2, directory.FullName);
            directory.Delete(true);
        }
Пример #6
0
        /// <summary>
        /// Automatically inspect assemblies in the install path and load/instantiate all items of type T.
        /// This method will exclude NotAutoReflected assemblies.  These assemblies are only loaded on-demand so as
        /// to avoid TypeLoad exceptions from missing types.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private static IEnumerable <T> AutoLoadExtensions <T>()
        {
            Log.Info($"Loading extensions of type {typeof(T)} from folder: {_installPathExtensionsDirectory}");

            var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <T>(_autoReflectedAssemblies);

            foreach (var ex in result.Exceptions)
            {
                Log.Warn($"An exception occurred while loading an extension: {ex}");
            }

            return(result.Instances);
        }
        public void when_invalid_file_on_disk()
        {
            var             directory  = MakeTempDirForTest();
            Action <string> testMethod = directoryPath =>
            {
                var filePath1 = Path.Combine(directoryPath, "foo.dll");
                File.CreateText(filePath1);
                var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <IInterface>(filePath1);
                Assert.AreEqual(0, result.Instances.Count());
                Assert.AreEqual(0, result.Exceptions.Count());
            };

            AppDomainExtensions.IsolateMethodInAppDomain(testMethod, directory.FullName);
            directory.Delete(true);
        }
        public void when_finds_files_on_disk()
        {
            var             directory  = MakeTempDirForTest();
            Action <string> testMethod = directoryPath =>
            {
                var filePath1 = Path.Combine(directoryPath, "foo.dll");
                var filePath2 = Path.Combine(directoryPath, "bar.dll");
                GenerateAssembly(@"public class Foo : IInterface {}", filePath1);
                GenerateAssembly(@"public class Bar : IInterface {}", filePath2);
                var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths <IInterface>(filePath1, filePath2);
                Assert.AreEqual(2, result.Instances.Count());
                Assert.AreEqual(0, result.Exceptions.Count());
            };

            AppDomainExtensions.IsolateMethodInAppDomain(testMethod, directory.FullName);
            directory.Delete(true);
        }