2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security / NamedPermissionSet.cs
blob85785bc0ec4b889da7689f632aba6c7c0c0fc8cb
1 //
2 // System.Security.NamedPermissionSet
3 //
4 // Authors:
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2002
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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 {
37 [ComVisible (true)]
38 [Serializable]
39 public sealed class NamedPermissionSet : PermissionSet {
41 private string name;
42 private string description;
44 // for PolicyLevel (to avoid validation duplication)
45 internal NamedPermissionSet ()
46 : base ()
50 public NamedPermissionSet (string name, PermissionSet permSet)
51 : base (permSet)
53 Name = name;
56 public NamedPermissionSet (string name, PermissionState state)
57 : base (state)
59 Name = name;
62 public NamedPermissionSet (NamedPermissionSet permSet)
63 : base (permSet)
65 name = permSet.name; // name can be null here
66 description = permSet.description;
69 public NamedPermissionSet (string name)
70 : this (name, PermissionState.Unrestricted)
74 // properties
76 public string Description {
77 get { return description; }
78 set { description = value; }
81 public string Name {
82 get { return name; }
83 set {
84 if ((value == null) || (value == String.Empty)) {
85 throw new ArgumentException (Locale.GetText ("invalid name"));
87 name = value;
91 // methods
93 public override PermissionSet Copy ()
95 return new NamedPermissionSet (this);
98 public NamedPermissionSet Copy (string name)
100 NamedPermissionSet nps = new NamedPermissionSet (this);
101 nps.Name = name; // get the new name
102 return nps;
105 public override void FromXml (SecurityElement et)
107 base.FromXml (et);
108 // strangely it can import a null Name (bypassing property setter)
109 name = et.Attribute ("Name");
110 description = et.Attribute ("Description");
111 if (description == null)
112 description = String.Empty;
115 public override SecurityElement ToXml ()
117 SecurityElement se = base.ToXml ();
118 if (name != null)
119 se.AddAttribute ("Name", name);
120 if (description != null)
121 se.AddAttribute ("Description", description);
122 return se;
125 [ComVisible (false)]
126 public override bool Equals (object obj)
128 if (obj == null)
129 return false;
130 NamedPermissionSet nps = (obj as NamedPermissionSet);
131 if (nps == null)
132 return false;
133 // description isn't part of the comparaison
134 return ((name == nps.Name) && base.Equals (obj));
137 [ComVisible (false)]
138 public override int GetHashCode ()
140 int hc = base.GetHashCode ();
141 // name is part of the hash code (except when null)
142 if (name != null)
143 hc ^= name.GetHashCode ();
144 // description is never part of the hash code
145 return hc;