This documentation is archived and is not being maintained.

PolicyStatement Class

Represents the statement of a CodeGroup describing the permissions and other information that apply to code with a particular set of evidence. This class cannot be inherited.

Namespace:  System.Security.Policy
Assembly:  mscorlib (in mscorlib.dll)

public sealed class PolicyStatement : ISecurityEncodable, 

A PolicyStatement consists of a set of granted permissions, and possible special attributes for the code group.

Policy statements are typically used as the return value of a Resolve operation on a PolicyLevel.

The following code example shows the use of members of the PolicyStatement class

using System;
using System.Security;
using System.Security.Policy;
using System.Security.Principal;
using System.Security.Permissions;

class Members
    static void Main(string[] args)
        // Create two new policy statements.
        PolicyStatement policyStatement = firstConstructorTest();
        PolicyStatement policyStatement2 = secondConstructorTest();

        // Add attributes to the first policy statement.
        policyStatement.Attributes = PolicyStatementAttribute.All;

        // Create a copy of the first policy statement.
        PolicyStatement policyStatementCopy = createCopy(policyStatement);
        addXmlMember(ref policyStatementCopy);

        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");

    // Construct a PolicyStatement with an Unrestricted permission set. 
    private static PolicyStatement firstConstructorTest() 
        // Construct the permission set.
        PermissionSet permissions 
            = new PermissionSet(PermissionState.Unrestricted);
            new SecurityPermission(SecurityPermissionFlag.Execution));
            new ZoneIdentityPermission(SecurityZone.MyComputer));

        // Create a policy statement based on the newly created permission 
        // set.
        PolicyStatement policyStatement = new PolicyStatement(permissions);

        return policyStatement;

    // Construct a PolicyStatement with an Unrestricted permission set and 
    // the LevelFinal attribute. 
    private static PolicyStatement secondConstructorTest()
        // Construct the permission set.
        PermissionSet permissions =
            new PermissionSet(PermissionState.Unrestricted);
            new SecurityPermission(SecurityPermissionFlag.Execution));
            new ZoneIdentityPermission(SecurityZone.MyComputer));

        PolicyStatementAttribute levelFinalAttribute = 

        // Create a new policy statement with the specified permission set. 
        // The LevelFinal attribute is set to prevent the evaluation of lower 
        // policy levels in a resolve operation.
        PolicyStatement policyStatement =
            new PolicyStatement(permissions, levelFinalAttribute);

        return policyStatement;

    // Add a named permission set to the specified PolicyStatement. 
    private static void AddPermissions(ref PolicyStatement policyStatement)
        // Construct a NamedPermissionSet with basic permissions.
        NamedPermissionSet allPerms = new NamedPermissionSet("allPerms");
            new SecurityPermission(SecurityPermissionFlag.Execution));
            new ZoneIdentityPermission(SecurityZone.MyComputer));
            new SiteIdentityPermission(""));

        policyStatement.PermissionSet = allPerms;

    // If a class attribute is not found in the specified PolicyStatement, 
    // add a child XML element with an added class attribute. 
    private static void addXmlMember(ref PolicyStatement policyStatement) 
        SecurityElement xmlElement = policyStatement.ToXml();
        if (xmlElement.Attribute("class") == null)
            SecurityElement newElement = 
                new SecurityElement("PolicyStatement");
            newElement.AddAttribute("class", policyStatement.ToString());

            newElement.AddChild(new SecurityElement("PermissionSet"));


            Console.Write("Added the class attribute and modified its ");
            Console.WriteLine("version number.\n" + newElement.ToString());

    // Verify that the type of the specified object is a PolicyStatement type 
    // then create a copy of the object. 
    private static PolicyStatement createCopy(Object sourceObject)
        PolicyStatement returnedStatement = new PolicyStatement(null);
        // Compare specified object type with the PolicyStatement type. 
        if (sourceObject.GetType().Equals(typeof(PolicyStatement)))
            returnedStatement = getCopy((PolicyStatement)sourceObject);
            throw new ArgumentException("Expected the PolicyStatement type.");

        return returnedStatement;

    // Return a copy of the specified PolicyStatement if the result of the 
    // Copy command is an equivalent object. Otherwise, return the 
    // original PolicyStatement object. 
    private static PolicyStatement getCopy(PolicyStatement policyStatement)
        // Create an equivalent copy of the policy statement.
        PolicyStatement policyStatementCopy = policyStatement.Copy();

        // Compare the specified objects for equality. 
        if (!policyStatementCopy.Equals(policyStatement))
            return policyStatementCopy;
            return policyStatement;

    // Summarize the attributes of the specified PolicyStatement on the 
    // console window. 
    private static void summarizePolicyStatment(
        PolicyStatement policyStatement)
        // Retrieve the class path for policyStatement.
        string policyStatementClass = policyStatement.ToString();

        int hashCode = policyStatement.GetHashCode();

        string attributeString = "";
        // Retrieve the string representation of the PolicyStatement 
        // attributes. 
        if (policyStatement.AttributeString != null)
            attributeString = policyStatement.AttributeString;

        // Write a summary to the console window.
        Console.WriteLine("\n*** " + policyStatementClass + " summary ***");
        Console.Write("This PolicyStatement has been created with hash ");
        Console.Write("code(" + hashCode + ") ");

        Console.Write("and contains the following attributes: ");
// This sample produces the following output: 
// Added the class attribute and modified the version number. 
// <PolicyStatement class="System.Security.Policy.PolicyStatement" 
//                  version="1.1">
//    <PermissionSet/> 
// </PolicyStatement> 
// *** System.Security.Policy.PolicyStatement summary *** 
// PolicyStatement has been created with hash code(20) containing the 
// following attributes: Exclusive LevelFinal 
// This sample completed successfully; press Enter to exit.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0