PolicyStatementCollection Class

Namespace: System.Security.Policy

Assembly: mscorlib (in mscorlib.dll)

Inheritance: Object > PolicyStatementCollection

Represents a collection of CodeGroup policy statements.

The PolicyStatementCollection class contains a list of PolicyStatement objects. Each PolicyStatement object can contain evidence, permissions, and a common name. The collection provides methods for adding, removing, and accessing these statements.

Syntax


public sealed class PolicyStatementCollection : IEnumerable
        

Remarks

Policy statements are used to define security policies for code. Each policy statement is associated with a CodeGroup, which specifies the evidence that a piece of code must possess to be granted the permissions defined in the associated policy statement.

Note: In .NET Framework version 4, the code access security (CAS) functionality has been deprecated. While the types are still available for compatibility purposes, their behavior might be different or they might not be supported in future versions.

Members

Constructors

Name Description
PolicyStatementCollection() Initializes a new instance of the PolicyStatementCollection class.

Methods

Name Description
Add(PolicyStatement statement) Adds a PolicyStatement object to the collection.
Clear() Removes all PolicyStatement objects from the collection.
GetEnumerator() Returns an enumerator that iterates through the collection.
Remove(PolicyStatement statement) Removes a specified PolicyStatement object from the collection.

Properties

Name Description
Count Gets the number of PolicyStatement objects in the collection.
Item(Int32 index) Gets or sets the PolicyStatement object at the specified index.

Example

The following example demonstrates how to create a PolicyStatementCollection and add policy statements to it.


using System;
using System.Security;
using System.Security.Policy;

public class Example {
    public static void Main() {
        PolicyStatementCollection statements = new PolicyStatementCollection();

        // Create a simple permission set
        PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);

        // Create a policy statement
        PolicyStatement statement1 = new PolicyStatement(permSet, null);
        statements.Add(statement1);

        // Add another statement (example with specific permissions)
        PermissionSet specificPermSet = new PermissionSet();
        specificPermSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
        PolicyStatement statement2 = new PolicyStatement(specificPermSet, null);
        statements.Add(statement2);

        Console.WriteLine($"{statements.Count} policy statements added.");

        // Iterate and display statements (simplified for example)
        foreach (PolicyStatement stmt in statements) {
            Console.WriteLine($"- Permissions: {stmt.Permissions.ToXml().Element.OuterXml}");
        }
    }
}
        

Requirements

Client: Windows 7, Windows Vista SP1, Windows XP SP2, Windows Server 2008 SP1, Windows Server 2003 SP2.
Server: Windows Server 2008 SP1, Windows Server 2003 SP2.
Framework: .NET Framework 1.1, 2.0, 3.0, 3.5, 4, 4.5, 4.6, 4.7, 4.8

See Also

System.Security.Policy Namespace
PolicyStatement Class
CodeGroup Class
PermissionSet Class