The Labs \ Source Viewer \ SSCLI \ System.Security \ NamedPermissionSet

  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. // NamedPermissionSet.cs
  16. //
  17. // Extends PermissionSet to allow an associated name and description
  18. //
  19. namespace System.Security
  20. {
  21.    
  22.     using System;
  23.     using System.Security.Util;
  24.     using PermissionState = System.Security.Permissions.PermissionState;
  25.     using System.Runtime.Serialization;
  26.     [Serializable()]
  27.     [System.Runtime.InteropServices.ComVisible(true)]
  28.     public sealed class NamedPermissionSet : PermissionSet
  29.     {
  30.         // The name of this PermissionSet
  31.         private string m_name;
  32.        
  33.         // The description of this PermissionSet
  34.         private string m_description;
  35.         [OptionalField(VersionAdded = 2)]
  36.         internal string m_descrResource;
  37.        
  38.         internal NamedPermissionSet() : base()
  39.         {
  40.         }
  41.        
  42.         public NamedPermissionSet(string name) : base()
  43.         {
  44.             CheckName(name);
  45.             m_name = name;
  46.         }
  47.        
  48.         public NamedPermissionSet(string name, PermissionState state) : base(state)
  49.         {
  50.             CheckName(name);
  51.             m_name = name;
  52.         }
  53.        
  54.        
  55.         public NamedPermissionSet(string name, PermissionSet permSet) : base(permSet)
  56.         {
  57.             CheckName(name);
  58.             m_name = name;
  59.         }
  60.        
  61.         public NamedPermissionSet(NamedPermissionSet permSet) : base(permSet)
  62.         {
  63.             m_name = permSet.m_name;
  64.             m_description = permSet.Description;
  65.         }
  66.        
  67.         public string Name {
  68.             get { return m_name; }
  69.             set {
  70.                 CheckName(value);
  71.                 m_name = value;
  72.             }
  73.         }
  74.        
  75.         private static void CheckName(string name)
  76.         {
  77.             if (name == null || name.Equals(""))
  78.                 throw new ArgumentException(Environment.GetResourceString("Argument_NPMSInvalidName"));
  79.         }
  80.        
  81.         public string Description {
  82.             get {
  83.                 if (m_descrResource != null) {
  84.                     m_description = Environment.GetResourceString(m_descrResource);
  85.                     m_descrResource = null;
  86.                 }
  87.                 return m_description;
  88.             }
  89.            
  90.             set {
  91.                 m_description = value;
  92.                 m_descrResource = null;
  93.             }
  94.         }
  95.        
  96.         public override PermissionSet Copy()
  97.         {
  98.             return new NamedPermissionSet(this);
  99.         }
  100.        
  101.         public NamedPermissionSet Copy(string name)
  102.         {
  103.             NamedPermissionSet set = new NamedPermissionSet(this);
  104.             set.Name = name;
  105.             return set;
  106.         }
  107.        
  108.         public override SecurityElement ToXml()
  109.         {
  110.             SecurityElement elem = base.ToXml("System.Security.NamedPermissionSet");
  111.             // If you hit this assert then most likely you are trying to change the name of this class.
  112.             // This is ok as long as you change the hard coded string above and change the assert below.
  113.             BCLDebug.Assert(this.GetType().FullName.Equals("System.Security.NamedPermissionSet"), "Class name changed!");
  114.            
  115.             if (m_name != null && !m_name.Equals("")) {
  116.                 elem.AddAttribute("Name", SecurityElement.Escape(m_name));
  117.             }
  118.            
  119.             if (Description != null && !Description.Equals("")) {
  120.                 elem.AddAttribute("Description", SecurityElement.Escape(Description));
  121.             }
  122.            
  123.             return elem;
  124.         }
  125.        
  126.         public override void FromXml(SecurityElement et)
  127.         {
  128.             FromXml(et, false, false);
  129.         }
  130.        
  131.         internal override void FromXml(SecurityElement et, bool allowInternalOnly, bool ignoreTypeLoadFailures)
  132.         {
  133.             if (et == null)
  134.                 throw new ArgumentNullException("et");
  135.            
  136.             string elem;
  137.            
  138.             elem = et.Attribute("Name");
  139.             m_name = elem == null ? null : elem;
  140.            
  141.             elem = et.Attribute("Description");
  142.             m_description = (elem == null ? "" : elem);
  143.             m_descrResource = null;
  144.            
  145.             base.FromXml(et, allowInternalOnly, ignoreTypeLoadFailures);
  146.         }
  147.        
  148.         internal void FromXmlNameOnly(SecurityElement et)
  149.         {
  150.             // This function gets only the name for the permission set, ignoring all other info.
  151.            
  152.             string elem;
  153.            
  154.             elem = et.Attribute("Name");
  155.             m_name = (elem == null ? null : elem);
  156.         }
  157.        
  158.         // NamedPermissionSet Equals should have the exact semantic as PermissionSet.
  159.         // We explicitly override them here to make sure that no one accidently
  160.         // changes this.
  161.        
  162.         [System.Runtime.InteropServices.ComVisible(false)]
  163.         public override bool Equals(object obj)
  164.         {
  165.             return base.Equals(obj);
  166.         }
  167.        
  168.         [System.Runtime.InteropServices.ComVisible(false)]
  169.         public override int GetHashCode()
  170.         {
  171.             return base.GetHashCode();
  172.         }
  173.     }
  174.    
  175.    
  176. }

Developer Fusion