Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / security / policy / permissionrequestevidence.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 //  PermissionRequestEvidence.cs
7 // 
8 // <OWNER>[....]</OWNER>
9 //
10 //  Encapsulation of permission request as an evidence type.
11 //
12
13 namespace System.Security.Policy {
14     using System.Runtime.Remoting;
15     using System;
16     using System.IO;
17     using System.Security.Util;
18     using System.Collections;
19     using System.Runtime.Serialization;
20     using System.Diagnostics.Contracts;
21
22     [Serializable]
23     [System.Runtime.InteropServices.ComVisible(true)]
24     [Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
25     public sealed class PermissionRequestEvidence : EvidenceBase
26     {
27         private PermissionSet m_request;
28         private PermissionSet m_optional;
29         private PermissionSet m_denied;
30
31         // These fields are not used, they are here for serialization compatibility with Whidbey
32 #pragma warning disable 169
33         private String m_strRequest;
34         private String m_strOptional;
35         private String m_strDenied;
36 #pragma warning restore 169
37     
38         public PermissionRequestEvidence(PermissionSet request, PermissionSet optional, PermissionSet denied)
39         {
40             if (request == null)
41                 m_request = null;
42             else
43                 m_request = request.Copy();
44                 
45             if (optional == null)
46                 m_optional = null;
47             else
48                 m_optional = optional.Copy();
49                 
50             if (denied == null)
51                 m_denied = null;
52             else
53                 m_denied = denied.Copy();
54         }
55     
56         public PermissionSet RequestedPermissions
57         {
58             get
59             {
60                 return m_request;
61             }
62         }
63
64         public PermissionSet OptionalPermissions
65         {
66             get
67             {
68                 return m_optional;
69             }
70         }
71
72         public PermissionSet DeniedPermissions
73         {
74             get
75             {
76                 return m_denied;
77             }
78         }
79
80         public override EvidenceBase Clone()
81         {
82             return Copy();
83         }
84
85         public PermissionRequestEvidence Copy()
86         {
87             return new PermissionRequestEvidence(m_request, m_optional, m_denied);
88         }
89
90         internal SecurityElement ToXml() {
91             SecurityElement root = new SecurityElement( "System.Security.Policy.PermissionRequestEvidence" );
92             // If you hit this assert then most likely you are trying to change the name of this class. 
93             // This is ok as long as you change the hard coded string above and change the assert below.
94             Contract.Assert( this.GetType().FullName.Equals( "System.Security.Policy.PermissionRequestEvidence" ), "Class name changed!" );
95
96             root.AddAttribute( "version", "1" );
97             
98             SecurityElement elem;
99             
100             if (m_request != null)
101             {
102                 elem = new SecurityElement( "Request" );
103                 elem.AddChild( m_request.ToXml() );
104                 root.AddChild( elem );
105             }
106                 
107             if (m_optional != null)
108             {
109                 elem = new SecurityElement( "Optional" );
110                 elem.AddChild( m_optional.ToXml() );
111                 root.AddChild( elem );
112             }
113                 
114             if (m_denied != null)
115             {
116                 elem = new SecurityElement( "Denied" );
117                 elem.AddChild( m_denied.ToXml() );
118                 root.AddChild( elem );
119             }
120             
121             return root;
122         }
123
124         public override String ToString()
125         {
126             return ToXml().ToString();
127         }
128     }
129 }