示例#1
0
文件: Program.cs 项目: rats-gr/csrun
        private static void execute(Assembly asm, Params param)
        {
            var flags = BindingFlags.ExactBinding | BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly;
            List<MethodInfo> methods = new List<MethodInfo>();
            if (param.entryPointType != null)
            {
                var t = asm.GetType(param.entryPointType, false);
                if (t != null)
                {
                    var method = t.GetMethod(param.entryPointMethod, flags, null, new Type[] { typeof(string[]) }, null);
                    if (method != null)
                        methods.Add(method);
                }
            }
            else
            {
                foreach (var t in asm.GetTypes())
                {
                    var method = t.GetMethod("Main", flags, null, new Type[] { typeof(string[]) }, null);
                    if (method != null)
                        methods.Add(method);
                }
            }
            if (methods.Count != 1)
            {
                Console.Error.WriteLine("Entry point not found. Use -entry.");
                Environment.ExitCode = 1;
                return;
            }

            try
            {
                methods[0].Invoke(null, new object[] { param.scriptArgs });
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.ToString());
                Environment.ExitCode = 1;
            }
        }
示例#2
0
文件: Program.cs 项目: rats-gr/csrun
 static void Main(string[] args)
 {
     Params param = new Params(args);
     Assembly asm = compileSource(param);
     if (asm != null)
         execute(asm, param);
 }
示例#3
0
文件: Program.cs 项目: rats-gr/csrun
        private static Assembly compileSource(Params param)
        {
            var cp = new CompilerParameters();
            cp.ReferencedAssemblies.Add("mscorlib.dll");
            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("System.Core.dll");
            cp.GenerateInMemory = true;
            cp.CompilerOptions = "/optimize";
            cp.WarningLevel = 3;

            Dictionary<string, SourceFileDesc> files = new Dictionary<string, SourceFileDesc>();

            try
            {
                {
                    var file = Path.GetFullPath(param.sourceFile);
                    var sfd = new SourceFileDesc(file, getIncludeResolver(file), getImportResolver(file));
                    files.Add(file, sfd);
                }

                var l = loadIncludes(files, files.Values);
                while (l.Count > 0)
                {
                    foreach (var f in l)
                        files[f.sourceFilePath] = f;
                    l = loadIncludes(files, l);
                }

                foreach (var i in files.Values.SelectMany(x => x.imports))
                    cp.ReferencedAssemblies.Add(i);

                using (var csp = new CSharpCodeProvider())
                {
                    var sources = files.Values.Select(x => x.filePath).ToArray();
                    var res = csp.CompileAssemblyFromFile(cp, sources);
                    if (res.Errors.HasErrors)
                    {
                        foreach (CompilerError e in res.Errors)
                        {
                            var fd = files.Values.First(x => x.filePath.ToLower() == e.FileName.ToLower());
                            Console.Error.WriteLine(fd.sourceFilePath + "(" + (e.Line + fd.lineOffset) + "," + e.Column + ") : error " + e.ErrorNumber + ": " + e.ErrorText);
                        }
                        Environment.ExitCode = 1;
                        return null;
                    }
                    return res.CompiledAssembly;
                }
            }
            catch (FileNotFoundException e)
            {
                Console.Error.WriteLine("File not found: " + e.FileName);
                Environment.ExitCode = 1;
            }
            finally
            {
                foreach (var f in files.Values)
                    f.deleteTempFile();
            }
            return null;
        }