Exemplo n.º 1
0
        public static (IEnumerable <Type>?, string) GetCompiledApps(out CollectibleAssemblyLoadContext alc, string codeFolder, ILogger logger)
        {
            var assembly = GetCompiledAppAssembly(out alc, codeFolder, logger);

            if (assembly == null)
            {
                return(null, "Compile error");
            }

            return(assembly.GetTypesWhereSubclassOf <NetDaemonAppBase>(), string.Empty);
        }
Exemplo n.º 2
0
        public static Assembly GetCompiledAppAssembly(out CollectibleAssemblyLoadContext alc, string codeFolder, ILogger logger)
        {
            alc = new CollectibleAssemblyLoadContext();

            try
            {
                var compilation = GetCsCompilation(codeFolder);

                foreach (var syntaxTree in compilation.SyntaxTrees)
                {
                    if (Path.GetFileName(syntaxTree.FilePath) != "_EntityExtensions.cs")
                    {
                        WarnIfExecuteIsMissing(syntaxTree, compilation, logger);
                    }

                    InterceptAppInfo(syntaxTree, compilation);
                }

                using (var peStream = new MemoryStream())
                {
                    var emitResult = compilation.Emit(peStream: peStream);

                    if (emitResult.Success)
                    {
                        peStream.Seek(0, SeekOrigin.Begin);

                        return(alc !.LoadFromStream(peStream));
                    }
                    else
                    {
                        PrettyPrintCompileError(emitResult, logger);

                        return(null !);
                    }
                }
            }
            finally
            {
                alc.Unload();
                // Finally do cleanup and release memory
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }