Exemple #1
0
        /// <summary>
        /// Load currently known assemblies
        /// </summary>
        private void LoadAssemblies()
        {
            lock (Locker)
            {
                //Clear current
                LoadedAssemblies.Clear();

                //Get all from current assembly directory
                var executableLocation = Assembly.GetEntryAssembly().Location;
                var path = Path.Combine(Path.GetDirectoryName(executableLocation));
                LoadedAssemblies.AddRange(GetAssemblies(path));

                //Get all from plug-ins locations
                string custompath = Config.GlobalConfig.AssembliesPath;

                //Check if path exists for reporting
                if (!string.IsNullOrWhiteSpace(custompath) && !Directory.Exists(custompath))
                {
                    _log.Warn($"Cannot load custom assemblies path, path does not exist {custompath}");
                }

                //Load
                if (!string.IsNullOrWhiteSpace(custompath) && Directory.Exists(custompath) && path != custompath)
                {
                    var found = GetAssemblies(custompath);
                    LoadedAssemblies.AddRange(found.Where(x => !LoadedAssemblies.Select(n => n.Location).Contains(x.Location)));
                }

                //Load container
                Container = new ContainerConfiguration().WithAssemblies(LoadedAssemblies);
            }
        }
            public IEnumerable <string> GetAssemblyLocation(string connectionString)
            {
                Load(connectionString);

                if (LoadedAssemblies != null)
                {
                    return(LoadedAssemblies.Select(a => a.Location));
                }

                var directory = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);

                if (directory == null)
                {
                    return(Provider.GetLibraries());
                }
                return(Provider.GetLibraries().Select(l => Path.Combine(directory, l)));
            }
        private void Compile(IList <string> sourceFiles, string assemblyName, string assemblyPath)
        {
            var parseOptions = CSharpParseOptions.Default;

            parseOptions = parseOptions.WithPreprocessorSymbols("NETCORE");

            var syntaxTrees = sourceFiles
                              .Select(path => CSharpSyntaxTree.ParseText(
                                          File.ReadAllText(path), parseOptions, path, Encoding.UTF8))
                              .ToList();

            LoadAssembliesFromUsings(syntaxTrees);
            var references = LoadedAssemblies.Select(assembly => assembly.Location)
                             .Select(path => MetadataReference.CreateFromFile(path))
                             .ToList();
            var compilationOptions = new CSharpCompilationOptions(
                OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release);
            var withTopLevelBinderFlagsMethod = compilationOptions.GetType()
                                                .GetMethod("WithTopLevelBinderFlags", BindingFlags.Instance | BindingFlags.NonPublic);
            var binderFlagsType = withTopLevelBinderFlagsMethod.GetParameters()[0].ParameterType;

            compilationOptions = (CSharpCompilationOptions)withTopLevelBinderFlagsMethod.Invoke(
                compilationOptions,
                new object[] { binderFlagsType.GetField("IgnoreCorLibraryDuplicatedTypes").GetValue(binderFlagsType) });

            var compilation = CSharpCompilation.Create(assemblyName)
                              .WithOptions(compilationOptions)
                              .AddReferences(references)
                              .AddSyntaxTrees(syntaxTrees);
            var emitResult = compilation.Emit(assemblyPath, null);

            if (!emitResult.Success)
            {
                throw new Exception(string.Join("\r\n",
                                                emitResult.Diagnostics.Where(d => d.WarningLevel == 0)));
            }
        }
Exemple #4
0
 internal void InitialiseDependencyResolvers()
 {
     _dependencyResolvers = LoadedAssemblies.Select(x => new DependencyResolver(x.Value)).ToList();
 }