2 // System.Security.NamedPermissionSet
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
35 namespace System.Security {
41 public sealed class NamedPermissionSet : PermissionSet {
44 private string description;
46 // for PolicyLevel (to avoid validation duplication)
47 internal NamedPermissionSet ()
52 public NamedPermissionSet (string name, PermissionSet set)
58 public NamedPermissionSet (string name, PermissionState state)
64 public NamedPermissionSet (NamedPermissionSet set)
67 name = set.name; // name can be null here
68 description = set.description;
71 public NamedPermissionSet (string name)
72 : this (name, PermissionState.None)
78 public string Description {
79 get { return description; }
80 set { description = value; }
86 if ((value == null) || (value == String.Empty)) {
87 throw new ArgumentException (Locale.GetText ("invalid name"));
95 public override PermissionSet Copy ()
97 return new NamedPermissionSet (this);
100 public NamedPermissionSet Copy (string name)
102 NamedPermissionSet nps = new NamedPermissionSet (this);
103 nps.Name = name; // get the new name
107 public override void FromXml (SecurityElement e)
110 // strangely it can import a null Name (bypassing property setter)
111 name = e.Attribute ("Name");
112 description = e.Attribute ("Description");
113 if (description == null)
114 description = String.Empty;
117 public override SecurityElement ToXml ()
119 SecurityElement se = base.ToXml ();
121 se.AddAttribute ("Name", name);
122 if (description != null)
123 se.AddAttribute ("Description", description);
129 public override bool Equals (object obj)
133 NamedPermissionSet nps = (obj as NamedPermissionSet);
136 // description isn't part of the comparaison
137 return ((name == nps.Name) && base.Equals (obj));
141 public override int GetHashCode ()
143 int hc = base.GetHashCode ();
144 // name is part of the hash code (except when null)
146 hc ^= name.GetHashCode ();
147 // description is never part of the hash code