示例#1
0
        public static object JSEval(string JSCode, string Output, List <string> Assemblies)
        {
            JScriptCodeProvider codeProvider = new JScriptCodeProvider();
            ICodeCompiler       icc          = codeProvider.CreateCompiler();

            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = true;
            foreach (string s in Assemblies)
            {
                parameters.ReferencedAssemblies.Add(s);
            }
            parameters.OutputAssembly = Output;
            CompilerResults results = icc.CompileAssemblyFromSource(parameters, JSCode);

            if (results.Errors.Count > 0)
            {
                string Errors = string.Empty;
                foreach (CompilerError CompErr in results.Errors)
                {
                    Errors += "Line number " + CompErr.Line +
                              ", Error Number: " + CompErr.ErrorNumber +
                              ", '" + CompErr.ErrorText + ";" +
                              Environment.NewLine + Environment.NewLine;
                }
                return(Errors);
            }
            else
            {
                Process.Start(Output);
                return("Succes!");
            }
        }
示例#2
0
        public static void Run(string[] args)
        {
            //ScriptManager.RegisterStartupScript(Page, GetType(), "clicktest", "clicktest()", true);


            //ClientScript.RegisterStartupScript(this.GetType(), "clear", "<script>test()</script>");

            string Source = @"
package Test 
{ 
  class HelloWorld 
  { 
    function Hello(name) { return ""Hello, "" + name; }

    

  }
}";

            var provider   = new JScriptCodeProvider();
            var compiler   = provider.CreateCompiler();
            var parameters = new CompilerParameters {
                GenerateInMemory = true
            };
            var     results  = compiler.CompileAssemblyFromSource(parameters, Source);
            var     assembly = results.CompiledAssembly;
            dynamic instance = Activator.CreateInstance(assembly.GetType("Test.HelloWorld"));
            var     result   = instance.Hello("World");

            Console.WriteLine("Result: {0}", result);
        }
示例#3
0
        //构造函数
        static EvalMethod()
        {
            CodeDomProvider    _provider   = new JScriptCodeProvider();
            ICodeCompiler      _iCode      = _provider.CreateCompiler();
            CompilerParameters _parameters = new CompilerParameters();

            _parameters.GenerateInMemory = true;
            CompilerResults _result;

            _result = _iCode.CompileAssemblyFromSource(_parameters, _jsClass);

            Assembly _assembly = _result.CompiledAssembly;

            _evalType   = _assembly.GetType("theEval");
            _evalObject = Activator.CreateInstance(_evalType);
        }
示例#4
0
        public string onStartUp(string fn)
        {
            string Source = System.IO.File.ReadAllText(fn);

            var provider   = new JScriptCodeProvider();
            var compiler   = provider.CreateCompiler();
            var parameters = new CompilerParameters {
                GenerateInMemory = true
            };
            var     results  = compiler.CompileAssemblyFromSource(parameters, Source);
            var     assembly = results.CompiledAssembly;
            dynamic instance = Activator.CreateInstance(assembly.GetType("StartupConfiguration"));
            var     result   = instance.getStartup3DFile(this.app);

            string sr = result.ToString();

            return(sr);
        }
示例#5
0
        /// <summary>
        /// Generates an Assembly from a script filename
        /// </summary>
        /// <param name="filename">The filename of the script</param>
        /// <param name="references">Assembly references for the script</param>
        /// <returns>The generated assembly</returns>
        public Assembly CreateAssembly(string filename, IList references)
        {
            // ensure that compilerErrors is null
            compilerErrors = null;

            string extension = Path.GetExtension(filename);

            // Select the correct CodeDomProvider based on script file extension
            CodeDomProvider codeProvider = null;

            switch (extension)
            {
            case ".cs":
                codeProvider = new CSharpCodeProvider();
                break;

            case ".vb":
                codeProvider = new CSharpCodeProvider();
                break;

            case ".js":
                codeProvider = new JScriptCodeProvider();
                break;

            default:
                throw new InvalidOperationException("Script files must have a .cs, .vb, or .js extension, for C#, Visual Basic.NET, or JScript respectively.");
            }

            ICodeCompiler compiler = codeProvider.CreateCompiler();

            // Set compiler parameters
            CompilerParameters compilerParams = new CompilerParameters();

            compilerParams.CompilerOptions         = "/target:library /optimize";
            compilerParams.GenerateExecutable      = false;
            compilerParams.GenerateInMemory        = true;
            compilerParams.IncludeDebugInformation = false;

            compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
            compilerParams.ReferencedAssemblies.Add("System.dll");

            // Add custom references
            foreach (string reference in references)
            {
                if (!compilerParams.ReferencedAssemblies.Contains(reference))
                {
                    compilerParams.ReferencedAssemblies.Add(reference);
                }
            }

            // Do the compilation
            CompilerResults results = compiler.CompileAssemblyFromFile(compilerParams,
                                                                       filename);

            //Do we have any compiler errors
            if (results.Errors.Count > 0)
            {
                compilerErrors = results.Errors;
                throw new Exception(
                          "Compiler error(s) encountered and saved to AssemblyFactory.CompilerErrors");
            }

            Assembly createdAssembly = results.CompiledAssembly;

            return(createdAssembly);
        }