The Labs \ Source Viewer \ SSCLI \ System.Security.Policy \ ApplicationDirectory

  1. // ==++==
  2. //
  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. //
  14. // ==--==
  15. // ApplicationDirectory.cs
  16. //
  17. // ApplicationDirectory is an evidence type representing the directory the assembly
  18. // was loaded from.
  19. //
  20. namespace System.Security.Policy
  21. {
  22.    
  23.     using System;
  24.     using System.IO;
  25.     using System.Security.Util;
  26.     using System.Collections;
  27.    
  28.     [Serializable()]
  29.     [System.Runtime.InteropServices.ComVisible(true)]
  30.     public sealed class ApplicationDirectory : IBuiltInEvidence
  31.     {
  32.         private URLString m_appDirectory;
  33.        
  34.         internal ApplicationDirectory()
  35.         {
  36.             m_appDirectory = null;
  37.         }
  38.        
  39.         public ApplicationDirectory(string name)
  40.         {
  41.             if (name == null)
  42.                 throw new ArgumentNullException("name");
  43.            
  44.             m_appDirectory = new URLString(name);
  45.         }
  46.        
  47.         public string Directory {
  48.             get { return m_appDirectory.ToString(); }
  49.         }
  50.        
  51.         public override bool Equals(object o)
  52.         {
  53.             if (o == null)
  54.                 return false;
  55.            
  56.             if (o is ApplicationDirectory) {
  57.                 ApplicationDirectory appDir = (ApplicationDirectory)o;
  58.                
  59.                 if (this.m_appDirectory == null) {
  60.                     return appDir.m_appDirectory == null;
  61.                 }
  62.                 else if (appDir.m_appDirectory == null) {
  63.                     return false;
  64.                 }
  65.                 else {
  66.                     return this.m_appDirectory.IsSubsetOf(appDir.m_appDirectory) && appDir.m_appDirectory.IsSubsetOf(this.m_appDirectory);
  67.                 }
  68.             }
  69.             return false;
  70.         }
  71.        
  72.         public override int GetHashCode()
  73.         {
  74.             return this.Directory.GetHashCode();
  75.         }
  76.        
  77.         public object Copy()
  78.         {
  79.             ApplicationDirectory appDir = new ApplicationDirectory();
  80.            
  81.             appDir.m_appDirectory = this.m_appDirectory;
  82.            
  83.             return appDir;
  84.         }
  85.        
  86.         internal SecurityElement ToXml()
  87.         {
  88.             SecurityElement root = new SecurityElement("System.Security.Policy.ApplicationDirectory");
  89.             // If you hit this assert then most likely you are trying to change the name of this class.
  90.             // This is ok as long as you change the hard coded string above and change the assert below.
  91.             BCLDebug.Assert(this.GetType().FullName.Equals("System.Security.Policy.ApplicationDirectory"), "Class name changed!");
  92.            
  93.             root.AddAttribute("version", "1");
  94.            
  95.             if (m_appDirectory != null)
  96.                 root.AddChild(new SecurityElement("Directory", m_appDirectory.ToString()));
  97.            
  98.             return root;
  99.         }
  100.        
  101.         /// <internalonly/>
  102.         int IBuiltInEvidence.OutputToBuffer(char[] buffer, int position, bool verbose)
  103.         {
  104.             buffer[position++] = BuiltInEvidenceHelper.idApplicationDirectory;
  105.             string directory = this.Directory;
  106.             int length = directory.Length;
  107.            
  108.             if (verbose) {
  109.                 BuiltInEvidenceHelper.CopyIntToCharArray(length, buffer, position);
  110.                 position += 2;
  111.             }
  112.             directory.CopyTo(0, buffer, position, length);
  113.             return length + position;
  114.         }
  115.        
  116.         /// <internalonly/>
  117.         int IBuiltInEvidence.InitFromBuffer(char[] buffer, int position)
  118.         {
  119.             int length = BuiltInEvidenceHelper.GetIntFromCharArray(buffer, position);
  120.             position += 2;
  121.            
  122.             m_appDirectory = new URLString(new string(buffer, position, length));
  123.            
  124.             return position + length;
  125.         }
  126.        
  127.         /// <internalonly/>
  128.         int IBuiltInEvidence.GetRequiredSize(bool verbose)
  129.         {
  130.             if (verbose)
  131.                 return this.Directory.Length + 3;
  132.             else
  133.                 // Directory + identifier + length
  134.                 return this.Directory.Length + 1;
  135.             // Directory + identifier
  136.         }
  137.        
  138.         public override string ToString()
  139.         {
  140.             return ToXml().ToString();
  141.         }
  142.     }
  143. }

Developer Fusion