DnsPermission.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // System.Net.DnsPermission.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Security;
  10. using System.Security.Permissions;
  11. namespace System.Net
  12. {
  13. [Serializable]
  14. public sealed class DnsPermission : CodeAccessPermission, IUnrestrictedPermission
  15. {
  16. // Fields
  17. bool m_noRestriction;
  18. // Constructors
  19. public DnsPermission (PermissionState state) : base ()
  20. {
  21. m_noRestriction = (state == PermissionState.Unrestricted);
  22. }
  23. // Methods
  24. public override IPermission Copy ()
  25. {
  26. // this is immutable.
  27. return this;
  28. }
  29. public override IPermission Intersect (IPermission target)
  30. {
  31. // LAMESPEC: says to throw an exception when null
  32. // but at same time it says to return null. We'll
  33. // follow MS behaviour.
  34. if (target == null)
  35. return null;
  36. DnsPermission perm = target as DnsPermission;
  37. if (perm == null)
  38. throw new ArgumentException ("Argument not of type DnsPermission");
  39. if (this.m_noRestriction && perm.m_noRestriction)
  40. return this;
  41. return this.m_noRestriction ? perm : this;
  42. }
  43. public override bool IsSubsetOf (IPermission target)
  44. {
  45. if (target == null)
  46. return !m_noRestriction;
  47. DnsPermission perm = target as DnsPermission;
  48. if (perm == null)
  49. throw new ArgumentException ("Argument not of type DnsPermission");
  50. return !this.m_noRestriction || perm.m_noRestriction;
  51. }
  52. public bool IsUnrestricted ()
  53. {
  54. return this.m_noRestriction;
  55. }
  56. /*
  57. DnsPermission dns1 = new DnsPermission (PermissionState.None);
  58. Console.WriteLine (dns1.ToXml ().ToString ());
  59. DnsPermission dns2 = new DnsPermission (PermissionState.Unrestricted);
  60. Console.WriteLine (dns2.ToXml ().ToString ());
  61. This is the sample xml output:
  62. <IPermission class="System.Net.DnsPermission, System, Version=1.0.3300.0, Cultur
  63. e=neutral, PublicKeyToken=b77a5c561934e089"
  64. version="1"/>
  65. <IPermission class="System.Net.DnsPermission, System, Version=1.0.3300.0, Cultur
  66. e=neutral, PublicKeyToken=b77a5c561934e089"
  67. version="1"
  68. Unrestricted="true"/>
  69. */
  70. public override SecurityElement ToXml ()
  71. {
  72. SecurityElement root = new SecurityElement ("IPermission");
  73. root.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
  74. root.AddAttribute ("version", "1");
  75. if (m_noRestriction)
  76. root.AddAttribute ("Unrestricted", "true");
  77. return root;
  78. }
  79. public override void FromXml (SecurityElement securityElement)
  80. {
  81. if (securityElement == null)
  82. throw new ArgumentNullException ("securityElement");
  83. // LAMESPEC: it says to throw an ArgumentNullException in this case
  84. if (securityElement.Tag != "IPermission")
  85. throw new ArgumentException ("securityElement");
  86. string classStr = securityElement.Attribute ("class");
  87. if (classStr == null || !classStr.StartsWith (this.GetType ().FullName + ","))
  88. throw new ArgumentException ("securityElement");
  89. string unrestricted = securityElement.Attribute ("Unrestricted");
  90. if (unrestricted != null)
  91. this.m_noRestriction = (String.Compare (unrestricted, "true", true) == 0);
  92. }
  93. public override IPermission Union (IPermission target)
  94. {
  95. // LAMESPEC: according to spec we should throw an
  96. // exception when target is null. We'll follow the
  97. // behaviour of MS.Net instead of the spec.
  98. if (target == null)
  99. return this;
  100. // throw new ArgumentNullException ("target");
  101. DnsPermission perm = target as DnsPermission;
  102. if (perm == null)
  103. throw new ArgumentException ("Argument not of type DnsPermission");
  104. return this.m_noRestriction ? this : perm;
  105. }
  106. }
  107. }