Beispiel #1
0
        public static T CreateScriptObject <T>(this ICompiler compiler, CompilationPackage compilationPackage, string typeName, bool throwExceptionOnCompileFailed)
            where T : class
        {
            var assembly = compiler.CompileToAssembly(compilationPackage, throwExceptionOnCompileFailed);

            return(assembly.CreateInstance(typeName) as T);
        }
Beispiel #2
0
        public static T CreateScriptObject <T>(this ICompiler compiler, CompilationPackage compilationPackage, bool throwExceptionOnCompileFailed)
            where T : class
        {
            var assembly = compiler.CompileToAssembly(compilationPackage, throwExceptionOnCompileFailed);

            var type = assembly.ExportedTypes.FirstOrDefault();

            if (type == null)
            {
                return(null);
            }

            return(Activator.CreateInstance(type) as T);
        }
Beispiel #3
0
        public static Assembly CompileToAssembly(this ICompiler compiler, CompilationPackage compilationPackage, bool throwExceptionOnCompileFailed)
        {
            using (var memoryStream = new MemoryStream())
            {
                var output = new CompilationOutput(CompilationOutputKind.DynamicallyLinkedLibrary, new StreamCompilationOutputData(memoryStream));

                var compilerResult = compiler.Compile(compilationPackage, output);

                if (compilerResult.Success)
                {
                    return(Assembly.Load(memoryStream.ToArray()));
                }

                if (throwExceptionOnCompileFailed)
                {
                    throw new CompileFailedException(compilerResult.Messages);
                }
            }

            return(null);
        }