NetCodeGroup Class

System.Security.Policy Namespace

Overview

The NetCodeGroup class represents a code group that contains other code groups. This class is the base class for code groups that are composed of child code groups.

Code groups are used in the .NET Framework security policy to grant permissions to code based on various evidence. The NetCodeGroup provides a hierarchical structure for organizing these permissions.

Key features of NetCodeGroup include:

  • Ability to contain child code groups, forming a tree-like structure.
  • Methods for adding, removing, and retrieving child code groups.
  • Overrideable methods for evaluating code against the policy.

Syntax

Namespace: System.Security.Policy

Assembly: mscorlib.dll


public sealed class NetCodeGroup : CodeGroup
{
    // Constructors
    public NetCodeGroup(IMembershipCondition membershipCondition, PolicyStatement policy);

    // Methods
    public override void AddChild(CodeGroup group);
    public override CodeGroup Copy();
    public override string GetInformation();
    public override PolicyStatementhesize(PolicyStatement parentPolicy);
    public override CodeGroup Resolve(Evidence evidence);
    public override void FromXml(SecurityElement e, PolicyLevel level);
    public override SecurityElement ToXml();
}
                

Inheritance Hierarchy


System.Object
  System.Security.CodeAGroup
    System.Security.Policy.NetCodeGroup
                

Members

Name Description
Constructor Initializes a new instance of the NetCodeGroup class.
AddChild(CodeGroup group) Adds a child code group to the current code group.
Copy() Creates and returns a deep copy of the current code group.
GetInformation() Gets a string representation of the current code group.
PolicyStatementhesize(PolicyStatement parentPolicy) Combines the policy statement of the current code group with the policy statement of the parent code group.
Resolve(Evidence evidence) Resolves the code group based on the provided evidence.
FromXml(SecurityElement e, PolicyLevel level) Recreates a code group from an XML encoding.
ToXml() Converts a code group into an XML encoding.

Example

C# Code Example

This example demonstrates creating a simple NetCodeGroup and adding a child code group.


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

public class NetCodeGroupExample
{
    public static void Main(string[] args)
    {
        // Create a membership condition (e.g., based on URL)
        UrlMembershipCondition urlCondition = new UrlMembershipCondition("http://www.example.com/*");

        // Create a policy statement (e.g., granting full trust)
        PolicyStatement fullTrustStatement = new PolicyStatement(new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted));

        // Create the parent NetCodeGroup
        NetCodeGroup parentGroup = new NetCodeGroup(urlCondition, fullTrustStatement);

        // Create a child code group (e.g., a FileCodeGroup)
        FileCodeGroup childGroup = new FileCodeGroup(new UrlMembershipCondition("file://c:/MyApp/*"), new PolicyStatement(new PermissionSet()));

        // Add the child group to the parent
        parentGroup.AddChild(childGroup);

        Console.WriteLine("NetCodeGroup created successfully with a child group.");
        Console.WriteLine("Parent Group Information: " + parentGroup.GetInformation());
    }
}
                    

Requirements

Namespace: System.Security.Policy

Assembly: mscorlib.dll

Runtime: .NET Framework 1.1, .NET Framework 2.0, .NET Framework 3.0, .NET Framework 3.5, .NET Framework 4.0, .NET Framework 4.5, .NET Framework 4.6, .NET Framework 4.7, .NET Framework 4.8