The Labs \ Source Viewer \ SSCLI \ System.CodeDom.Compiler \ CompilerResults

  1. //------------------------------------------------------------------------------
  2. // <copyright file="CompilerResults.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.CodeDom.Compiler
  16. {
  17.     using System;
  18.     using System.CodeDom;
  19.     using System.Reflection;
  20.     using System.Collections;
  21.     using System.Collections.Specialized;
  22.     using System.Security;
  23.     using System.Security.Permissions;
  24.     using System.Security.Policy;
  25.     using System.Runtime.Serialization;
  26.     using System.Runtime.Serialization.Formatters.Binary;
  27.     using System.IO;
  28.    
  29.    
  30.     /// <devdoc>
  31.     /// <para>
  32.     /// Represents the results
  33.     /// of compilation from the compiler.
  34.     /// </para>
  35.     /// </devdoc>
  36.     [Serializable()]
  37.     [PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
  38.     public class CompilerResults
  39.     {
  40.         private CompilerErrorCollection errors = new CompilerErrorCollection();
  41.         private StringCollection output = new StringCollection();
  42.         private Assembly compiledAssembly;
  43.         private string pathToAssembly;
  44.         private int nativeCompilerReturnValue;
  45.         private TempFileCollection tempFiles;
  46.         private Evidence evidence;
  47.        
  48.         /// <devdoc>
  49.         /// <para>
  50.         /// Initializes a new instance of <see cref='System.CodeDom.Compiler.CompilerResults'/>
  51.         /// that uses the specified
  52.         /// temporary files.
  53.         /// </para>
  54.         /// </devdoc>
  55.         [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  56.         public CompilerResults(TempFileCollection tempFiles)
  57.         {
  58.             this.tempFiles = tempFiles;
  59.         }
  60.        
  61.         /// <devdoc>
  62.         /// <para>
  63.         /// Gets or sets the temporary files to use.
  64.         /// </para>
  65.         /// </devdoc>
  66.         public TempFileCollection TempFiles {
  67.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  68.             get { return tempFiles; }
  69.            
  70.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  71.             set { tempFiles = value; }
  72.         }
  73.        
  74.         /// <devdoc>
  75.         /// <para>
  76.         /// Set the evidence for partially trusted scenarios.
  77.         /// </para>
  78.         /// </devdoc>
  79.         public Evidence Evidence {
  80.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  81.             get {
  82.                 Evidence e = null;
  83.                 if (evidence != null)
  84.                     e = CloneEvidence(evidence);
  85.                 return e;
  86.             }
  87.            
  88.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  89.             [SecurityPermissionAttribute(SecurityAction.Demand, ControlEvidence = true)]
  90.             set {
  91.                 if (value != null)
  92.                     evidence = CloneEvidence(value);
  93.                 else
  94.                     evidence = null;
  95.             }
  96.         }
  97.        
  98.         /// <devdoc>
  99.         /// <para>
  100.         /// The compiled assembly.
  101.         /// </para>
  102.         /// </devdoc>
  103.         public Assembly CompiledAssembly {
  104.             [SecurityPermissionAttribute(SecurityAction.Assert, Flags = SecurityPermissionFlag.ControlEvidence)]
  105.             get {
  106.                 if (compiledAssembly == null && pathToAssembly != null) {
  107.                     AssemblyName assemName = new AssemblyName();
  108.                     assemName.CodeBase = pathToAssembly;
  109.                     compiledAssembly = Assembly.Load(assemName, evidence);
  110.                 }
  111.                 return compiledAssembly;
  112.             }
  113.            
  114.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  115.             set { compiledAssembly = value; }
  116.         }
  117.        
  118.         /// <devdoc>
  119.         /// <para>
  120.         /// Gets or sets the collection of compiler errors.
  121.         /// </para>
  122.         /// </devdoc>
  123.         public CompilerErrorCollection Errors {
  124.             get { return errors; }
  125.         }
  126.        
  127.         /// <devdoc>
  128.         /// <para>
  129.         /// Gets or sets the compiler output messages.
  130.         /// </para>
  131.         /// </devdoc>
  132.         public StringCollection Output {
  133.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  134.             get { return output; }
  135.         }
  136.        
  137.         /// <devdoc>
  138.         /// <para>
  139.         /// Gets or sets the path to the assembly.
  140.         /// </para>
  141.         /// </devdoc>
  142.         public string PathToAssembly {
  143.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  144.             get { return pathToAssembly; }
  145.            
  146.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  147.             set { pathToAssembly = value; }
  148.         }
  149.        
  150.         /// <devdoc>
  151.         /// <para>
  152.         /// Gets or sets the compiler's return value.
  153.         /// </para>
  154.         /// </devdoc>
  155.         public int NativeCompilerReturnValue {
  156.             get { return nativeCompilerReturnValue; }
  157.            
  158.             [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  159.             set { nativeCompilerReturnValue = value; }
  160.         }
  161.        
  162.         static internal Evidence CloneEvidence(Evidence ev)
  163.         {
  164.             new PermissionSet(PermissionState.Unrestricted).Assert();
  165.            
  166.             MemoryStream stream = new MemoryStream();
  167.            
  168.             BinaryFormatter formatter = new BinaryFormatter();
  169.            
  170.             formatter.Serialize(stream, ev);
  171.            
  172.             stream.Position = 0;
  173.            
  174.             return (Evidence)formatter.Deserialize(stream);
  175.         }
  176.     }
  177. }

Developer Fusion