// </Snippet1> // Create a DnsPermission instance that is the intersection of current // DnsPermission instance and the specified DnsPermission instance. private void MyIntersection() { DnsPermission permission = (DnsPermission)dnsPermission1.Intersect(dnsPermission2); // Print the attributes and the values of the intersection instance of // DnsPermission. PrintKeysAndValues(permission.ToXml().Attributes); }
// <Snippet1> private void MyUnion() { // Create a DnsPermission instance that is the union of the current DnsPermission // instance and the specified DnsPermission instance. DnsPermission permission = (DnsPermission)dnsPermission1.Union(dnsPermission2); // Print the attributes and the values of the union instance of DnsPermission. PrintKeysAndValues(permission.ToXml().Attributes); }
public void FromXml_WrongVersion() { DnsPermission dp = new DnsPermission(PermissionState.None); SecurityElement se = dp.ToXml(); se.Attributes.Remove("version"); se.Attributes.Add("version", "2"); dp.FromXml(se); }
public void FromXml_WrongTagCase() { DnsPermission dp = new DnsPermission(PermissionState.None); SecurityElement se = dp.ToXml(); se.Tag = "IPERMISSION"; // instead of IPermission dp.FromXml(se); // note: normally IPermission classes (in corlib) DO care about the // IPermission tag }
public void FromXml_NoVersion() { DnsPermission dp = new DnsPermission(PermissionState.None); SecurityElement se = dp.ToXml(); SecurityElement w = new SecurityElement(se.Tag); w.AddAttribute("class", se.Attribute("class")); dp.FromXml(w); }
public void FromXml_WrongTag() { DnsPermission dp = new DnsPermission(PermissionState.None); SecurityElement se = dp.ToXml(); se.Tag = "IMono"; dp.FromXml(se); // note: normally IPermission classes (in corlib) DO care about the // IPermission tag }
// <Snippet1> public void ConstructDnsPermission() { try { // Create a DnsPermission instance. DnsPermission permission = new DnsPermission(PermissionState.None); // Create a SecurityElement instance by calling the ToXml method on the // DnsPermission instance. // Print its attributes, which hold the XML encoding of the DnsPermission // instance. Console.WriteLine("Attributes and Values of 'DnsPermission' instance :"); PrintKeysAndValues(permission.ToXml().Attributes); // Create a SecurityElement instance. SecurityElement securityElementObj = new SecurityElement("IPermission"); // Add attributes and values of the SecurityElement instance corresponding to // the permission instance. securityElementObj.AddAttribute("version", "1"); securityElementObj.AddAttribute("Unrestricted", "true"); securityElementObj.AddAttribute("class", "System.Net.DnsPermission"); // Reconstruct a DnsPermission instance from an XML encoding. DnsPermission permission1 = new DnsPermission(PermissionState.None); permission1.FromXml(securityElementObj); // Print the attributes and values of the constructed DnsPermission object. Console.WriteLine("After reconstruction Attributes and Values of new DnsPermission instance :"); PrintKeysAndValues(permission1.ToXml().Attributes); } catch (NullReferenceException e) { Console.WriteLine("NullReferenceException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch (SecurityException e) { Console.WriteLine("SecurityException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch (Exception e) { Console.WriteLine("Exception caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } }
public static void Main(string[] args) { DnsPermission permission = new DnsPermission(System.Security.Permissions.PermissionState.Unrestricted); permission.Demand(); //创建一个SecurityElement对象来控制DnsPermission实例 SecurityElement securityElementObj = permission.ToXml(); Console.WriteLine("Tag,Attributes and Values of 'DnsPermission' instance :"); //输出属性名称和值 PrintKeysAndValues(securityElementObj.Attributes); }
public void FromXml_NoClass() { DnsPermission dp = new DnsPermission(PermissionState.None); SecurityElement se = dp.ToXml(); SecurityElement w = new SecurityElement(se.Tag); w.AddAttribute("version", se.Attribute("version")); dp.FromXml(w); // note: normally IPermission classes (in corlib) DO NOT care about // attribute "class" name presence in the XML }
public void FromXml_WrongClass() { DnsPermission dp = new DnsPermission(PermissionState.None); SecurityElement se = dp.ToXml(); SecurityElement w = new SecurityElement(se.Tag); w.AddAttribute("class", "Wrong" + se.Attribute("class")); w.AddAttribute("version", se.Attribute("version")); dp.FromXml(w); // doesn't care of the class name at that stage // anyway the class has already be created so... }
// <Snippet1> public void useDns() { // Create a DnsPermission instance. DnsPermission permission = new DnsPermission(PermissionState.Unrestricted); // Check for permission. permission.Demand(); // Create a SecurityElement object to hold XML encoding of the DnsPermission instance. SecurityElement securityElementObj = permission.ToXml(); Console.WriteLine("Tag, Attributes and Values of 'DnsPermission' instance :"); Console.WriteLine("\n\tTag :" + securityElementObj.Tag); // Print the attributes and values. PrintKeysAndValues(securityElementObj.Attributes); }
// <Snippet1> public void useDns() { // Create a DnsPermission instance. permission = new DnsPermission(PermissionState.Unrestricted); DnsPermission dnsPermission1 = new DnsPermission(PermissionState.None); // Check for permission. permission.Demand(); dnsPermission1.Demand(); // Print the attributes and values. Console.WriteLine("Attributes and Values of 'DnsPermission' instance :"); PrintKeysAndValues(permission.ToXml().Attributes); Console.WriteLine("Attributes and Values of specified 'DnsPermission' instance :"); PrintKeysAndValues(dnsPermission1.ToXml().Attributes); Subset(dnsPermission1); }
// <Snippet1> public void UseDns() { // Create a DnsPermission instance. DnsPermission myPermission = new DnsPermission(PermissionState.Unrestricted); // Check for permission. myPermission.Demand(); // Create an identical copy of the above 'DnsPermission' object. DnsPermission myPermissionCopy = (DnsPermission)myPermission.Copy(); Console.WriteLine("Attributes and Values of 'DnsPermission' instance :"); // Print the attributes and values. PrintKeysAndValues(myPermission.ToXml().Attributes); Console.WriteLine("Attribute and values of copied instance :"); PrintKeysAndValues(myPermissionCopy.ToXml().Attributes); }
// <Snippet2> public void useDns() { // Create a DnsPermission instance. dnsPermission1 = new DnsPermission(PermissionState.Unrestricted); dnsPermission2 = new DnsPermission(PermissionState.None); // Check for permission. dnsPermission1.Demand(); dnsPermission2.Demand(); Console.WriteLine("Attributes and Values of first DnsPermission instance :"); PrintKeysAndValues(dnsPermission1.ToXml().Attributes); Console.WriteLine("Attributes and Values of second DnsPermission instance :"); PrintKeysAndValues(dnsPermission2.ToXml().Attributes); Console.WriteLine("Union of both instances : "); MyUnion(); Console.WriteLine("Intersection of both instances : "); MyIntersection(); }
public void PermissionState_None() { PermissionState ps = PermissionState.None; DnsPermission dp = new DnsPermission(ps); Assert.IsFalse(dp.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = dp.ToXml(); // only class and version are present Assert.AreEqual(2, se.Attributes.Count, "Xml-Attributes#"); Assert.IsNull(se.Children, "Xml-Children"); DnsPermission copy = (DnsPermission)dp.Copy(); Assert.IsFalse(Object.ReferenceEquals(dp, copy), "ReferenceEquals"); Assert.AreEqual(dp.IsUnrestricted(), copy.IsUnrestricted(), "IsUnrestricted ()"); }
public void PermissionState_Unrestricted() { PermissionState ps = PermissionState.Unrestricted; DnsPermission dp = new DnsPermission(ps); Assert.IsTrue(dp.IsUnrestricted(), "IsUnrestricted"); SecurityElement se = dp.ToXml(); Assert.AreEqual("true", se.Attribute("Unrestricted"), "Xml-Unrestricted"); Assert.AreEqual(3, se.Attributes.Count, "Xml-Attributes#"); Assert.IsNull(se.Children, "Xml-Children"); DnsPermission copy = (DnsPermission)dp.Copy(); Assert.IsFalse(Object.ReferenceEquals(dp, copy), "ReferenceEquals"); Assert.AreEqual(dp.IsUnrestricted(), copy.IsUnrestricted(), "IsUnrestricted ()"); }
// <Snippet1> public void useDns() { // Create a DnsPermission instance. DnsPermission permission = new DnsPermission(PermissionState.Unrestricted); // Check for permission. permission.Demand(); Console.WriteLine("Attributes and Values of DnsPermission instance :"); // Print the attributes and values. PrintKeysAndValues(permission.ToXml().Attributes); // Check the permission state. if (permission.IsUnrestricted()) { Console.WriteLine("Overall permissions : Unrestricted"); } else { Console.WriteLine("Overall permissions : Restricted"); } }