Exemple #1
0
        /// <include file='doc\CodeCompiler.uex' path='docs/doc[@for="CodeCompiler.FromSourceBatch"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Compiles the specified source code strings using the specified options, and
        ///       returns the results from the compilation.
        ///    </para>
        /// </devdoc>
        protected virtual CompilerResults FromSourceBatch(CompilerParameters options, string[] sources)
        {
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

            string[] filenames = new string[sources.Length];

            IntPtr          impersonatedToken = IntPtr.Zero;
            CompilerResults results           = null;
            bool            needToImpersonate = Executor.RevertImpersonation(options.UserToken, ref impersonatedToken);

            try {
                for (int i = 0; i < sources.Length; i++)
                {
                    string name = options.TempFiles.AddExtension(i + FileExtension);
                    Stream temp = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.Read);
                    try {
                        StreamWriter sw = new StreamWriter(temp, Encoding.UTF8);
                        sw.Write(sources[i]);
                        sw.Flush();
                        sw.Close();
                    }
                    finally {
                        temp.Close();
                    }
                    filenames[i] = name;
                }
                results = FromFileBatch(options, filenames);
            }
            finally {
                Executor.ReImpersonate(impersonatedToken, needToImpersonate);
            }
            return(results);
        }
        protected virtual CompilerResults FromDomBatch(CompilerParameters options, CodeCompileUnit[] ea)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (ea == null)
            {
                throw new ArgumentNullException("ea");
            }

            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

            string[] filenames = new string[ea.Length];

            CompilerResults results = null;

#if !FEATURE_PAL
            // the extra try-catch is here to mitigate exception filter injection attacks.
            try {
                WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
                try {
#endif // !FEATURE_PAL
            for (int i = 0; i < ea.Length; i++)
            {
                if (ea[i] == null)
                {
                    continue;               // the other two batch methods just work if one element is null, so we'll match that.
                }
                ResolveReferencedAssemblies(options, ea[i]);
                filenames[i] = options.TempFiles.AddExtension(i + FileExtension);
                Stream temp = new FileStream(filenames[i], FileMode.Create, FileAccess.Write, FileShare.Read);
                try {
                    using (StreamWriter sw = new StreamWriter(temp, Encoding.UTF8)){
                        ((ICodeGenerator)this).GenerateCodeFromCompileUnit(ea[i], sw, Options);
                        sw.Flush();
                    }
                }
                finally {
                    temp.Close();
                }
            }

            results = FromFileBatch(options, filenames);
#if !FEATURE_PAL
        }

        finally {
            Executor.ReImpersonate(impersonation);
        }
    }
    catch {
        throw;
    }
#endif // !FEATURE_PAL
            return(results);
        }
Exemple #3
0
        protected virtual CompilerResults FromDomBatch(CompilerParameters options, CodeCompileUnit[] ea)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (ea == null)
            {
                throw new ArgumentNullException("ea");
            }
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
            string[]        fileNames = new string[ea.Length];
            CompilerResults results   = null;

            try
            {
                WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
                try
                {
                    for (int i = 0; i < ea.Length; i++)
                    {
                        if (ea[i] != null)
                        {
                            this.ResolveReferencedAssemblies(options, ea[i]);
                            fileNames[i] = options.TempFiles.AddExtension(i + this.FileExtension);
                            Stream stream = new FileStream(fileNames[i], FileMode.Create, FileAccess.Write, FileShare.Read);
                            try
                            {
                                using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                                {
                                    ((ICodeGenerator)this).GenerateCodeFromCompileUnit(ea[i], writer, base.Options);
                                    writer.Flush();
                                }
                            }
                            finally
                            {
                                stream.Close();
                            }
                        }
                    }
                    results = this.FromFileBatch(options, fileNames);
                }
                finally
                {
                    Executor.ReImpersonate(impersonation);
                }
            }
            catch
            {
                throw;
            }
            return(results);
        }
        protected virtual CompilerResults FromSourceBatch(CompilerParameters options, string[] sources)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (sources == null)
            {
                throw new ArgumentNullException("sources");
            }

            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

            string[] filenames = new string[sources.Length];

            CompilerResults results = null;

#if !FEATURE_PAL
            // the extra try-catch is here to mitigate exception filter injection attacks.
            try {
                WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
                try {
#endif // !FEATURE_PAL
            for (int i = 0; i < sources.Length; i++)
            {
                string name = options.TempFiles.AddExtension(i + FileExtension);
                Stream temp = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.Read);
                try {
                    using (StreamWriter sw = new StreamWriter(temp, Encoding.UTF8)) {
                        sw.Write(sources[i]);
                        sw.Flush();
                    }
                }
                finally {
                    temp.Close();
                }
                filenames[i] = name;
            }
            results = FromFileBatch(options, filenames);
#if !FEATURE_PAL
        }

        finally {
            Executor.ReImpersonate(impersonation);
        }
    }
    catch {
        throw;
    }
#endif // !FEATURE_PAL

            return(results);
        }
        internal void SafeDelete()
        {
            WindowsImpersonationContext impersonation = Executor.RevertImpersonation();

            try
            {
                this.Delete();
            }
            finally
            {
                Executor.ReImpersonate(impersonation);
            }
        }
Exemple #6
0
        protected virtual CompilerResults FromSourceBatch(CompilerParameters options, string[] sources)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (sources == null)
            {
                throw new ArgumentNullException("sources");
            }
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
            string[]        fileNames = new string[sources.Length];
            CompilerResults results   = null;

            try
            {
                WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
                try
                {
                    for (int i = 0; i < sources.Length; i++)
                    {
                        string path   = options.TempFiles.AddExtension(i + this.FileExtension);
                        Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
                        try
                        {
                            using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                            {
                                writer.Write(sources[i]);
                                writer.Flush();
                            }
                        }
                        finally
                        {
                            stream.Close();
                        }
                        fileNames[i] = path;
                    }
                    results = this.FromFileBatch(options, fileNames);
                }
                finally
                {
                    Executor.ReImpersonate(impersonation);
                }
            }
            catch
            {
                throw;
            }
            return(results);
        }
Exemple #7
0
        internal void SafeDelete()
        {
#if !FEATURE_PAL
            WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
#endif
            try{
                Delete();
            }
            finally {
#if !FEATURE_PAL
                Executor.ReImpersonate(impersonation);
#endif
            }
        }
Exemple #8
0
        public TempFileCollection(string tempDir, bool keepFiles)
        {
            this.keepFiles = keepFiles;
            this.tempDir   = tempDir;
#if !FEATURE_CASE_SENSITIVE_FILESYSTEM
            files = new Hashtable(StringComparer.OrdinalIgnoreCase);
#else
            files = new Hashtable();
#endif
#if !FEATURE_PAL
            WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
            try
            {
                currentIdentity = WindowsIdentity.GetCurrent();
            }
            finally
            {
                Executor.ReImpersonate(impersonation);
            }
#endif
        }
Exemple #9
0
        protected virtual CompilerResults FromSourceBatch(CompilerParameters options, string[] sources)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (sources == null)
            {
                throw new ArgumentNullException("sources");
            }

            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

            string[]     filenames   = new string[sources.Length];
            FileStream[] fileStreams = new FileStream[sources.Length];

            CompilerResults results = null;

#if !FEATURE_PAL
            // the extra try-catch is here to mitigate exception filter injection attacks.
            try {
                WindowsImpersonationContext impersonation = Executor.RevertImpersonation();
                try {
#endif // !FEATURE_PAL
            try {
                bool isFileIntegrityEnabled = FileIntegrity.IsEnabled;
                for (int i = 0; i < sources.Length; i++)
                {
                    string     name       = options.TempFiles.AddExtension(i + FileExtension);
                    FileStream fileStream = new FileStream(name, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
                    fileStreams[i] = fileStream;
                    using (StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8)) {
                        sw.Write(sources[i]);
                        sw.Flush();
                        if (isFileIntegrityEnabled)
                        {
                            FileIntegrity.MarkAsTrusted(fileStream.SafeFileHandle);
                        }
                    }
                    filenames[i] = name;
                }

                results = FromFileBatch(options, filenames);
            }
            finally {
                for (int i = 0; i < fileStreams.Length && fileStreams[i] != null; i++)
                {
                    fileStreams[i].Close();
                }
            }
#if !FEATURE_PAL
        }

        finally {
            Executor.ReImpersonate(impersonation);
        }
    }
    catch {
        throw;
    }
#endif // !FEATURE_PAL

            return(results);
        }