CodeGroupCollection Class
Represents a collection of CodeGroup objects.
Namespace: System.Net.Security.CAS
Assembly: System.Net.Security.dll
Syntax
public sealed class CodeGroupCollection
Remarks
The CodeGroupCollection class is used to store and manage a set of code groups. Each code group is associated with a specific Permission set and controls the permissions granted to code originating from a particular source. This collection is fundamental to the Code Access Security (CAS) model in .NET Framework.
You can iterate through the collection to examine or modify individual code groups. This class provides methods for adding, removing, and accessing code groups within the collection.
Examples
The following example demonstrates how to create a new CodeGroupCollection and add a custom code group to it. In a real-world scenario, you would typically retrieve existing code groups from the policy store rather than creating them from scratch.
// This is a simplified example for demonstration purposes. // In a real application, you would interact with the Code Access Security policy. using System; using System.Security; using System.Security.Policy; using System.Net.Security.CAS; // Assuming this namespace for custom CodeGroup public class Example { public static void Main(string[] args) { // Create a new permission set (e.g., granting unrestricted access) PermissionSet allPermissions = new PermissionSet(SecurityElement.FromString("<IPermission class=\"System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" version=\"1\" Flags=\"AllFlags\"/>")); // Create a new code group with a specific membership condition Url urlCondition = new Url("http://www.example.com/*"); MyCustomCodeGroup myCodeGroup = new MyCustomCodeGroup(urlCondition, allPermissions); // Create a CodeGroupCollection CodeGroupCollection codeGroupCollection = new CodeGroupCollection(); // Add the custom code group to the collection codeGroupCollection.Add(myCodeGroup); Console.WriteLine($"{codeGroupCollection.Count} code group(s) added."); // In a real scenario, you would then merge this collection into the active policy } } // A placeholder for a custom CodeGroup implementation for demonstration public class MyCustomCodeGroup : CodeGroup { public MyCustomCodeGroup(IConditionalterm term, PermissionSet permissionSet) : base(term, permissionSet) { } public override IPermission CreateIdentityPermission(Evidence evidence) { return null; } public override IEnumerator GetEnumerator() { yield return this; } }
Thread Safety
Public static (Shared in Visual Basic) members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.