Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

GacIdentityPermission Class

Note: This class is new in the .NET Framework version 2.0.

Defines the identity permission for files originating in the global assembly cache. This class cannot be inherited.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class GacIdentityPermission : CodeAccessPermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class GacIdentityPermission extends CodeAccessPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class GacIdentityPermission extends CodeAccessPermission

Files are either in the global assembly cache, or they are not. There are no variations to the permission granted, so all GacIdentityPermission objects are equal.

The following code example demonstrates the use of the GacIdentityPermission class.

using System;
using System.Security;
using System.Security.Permissions;

public class GacIdentityPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    private bool IsSubsetOfDemo()
    {
        try
        {
            GacIdentityPermission Gac1 = new GacIdentityPermission();
            GacIdentityPermission Gac2 = new GacIdentityPermission(PermissionState.None);
            if (Gac1.Equals(Gac2))
                Console.WriteLine("GacIdentityPermission() equals GacIdentityPermission(PermissionState.None).");
            if (Gac1.IsSubsetOf(Gac2))
            {
                Console.WriteLine(Gac1 + " is a subset of " + Gac2);
            }
            else
            {
                Console.WriteLine(Gac1 + " is not a subset of " + Gac2);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine ("An exception was thrown : " + e);
            return false;
        }
        return true;
    }
    // Union creates a new permission that is the union of the current permission
    // and the specified permission.
    private bool UnionDemo()
    {
        GacIdentityPermission Gac1 = new GacIdentityPermission(PermissionState.None);
        GacIdentityPermission Gac2 = new GacIdentityPermission();
        try
        {
            GacIdentityPermission p3 = (GacIdentityPermission)Gac1.Union(Gac2);

            if (p3 != null)
            {
                Console.WriteLine("The union of two GacIdentityPermissions was successful.");

            }
            else
            {
                Console.WriteLine("The union of two GacIdentityPermissions failed.");
                return false;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("An exception was thrown : " + e);
            return false;

        }

        return true;

    }
    // Intersect creates and returns a new permission that is the intersection of the
    // current permission and the specified permission.
    private bool IntersectDemo()
    {
        GacIdentityPermission Gac1 = new GacIdentityPermission();
        GacIdentityPermission Gac2 = new GacIdentityPermission();
        try
        {
            GacIdentityPermission p3 = (GacIdentityPermission)Gac1.Intersect(Gac2);
            if (p3 != null)
            {
                Console.WriteLine("The intersection of the two permissions = " + p3.ToString() + "\n");

            }
            else
            {
                Console.WriteLine("The intersection of the two permissions is null.\n");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("An exception was thrown : " + e);
            return false;

        }

        return true;

    }
    //Copy creates and returns an identical copy of the current permission.
    private bool CopyDemo()
    {

        GacIdentityPermission Gac1 = new GacIdentityPermission();
        GacIdentityPermission Gac2 = new GacIdentityPermission();
        Console.WriteLine("**************************************************************************");
        try
        {
            Gac2 = (GacIdentityPermission)Gac1.Copy();
            if (Gac2 != null)
            {
                Console.WriteLine("Result of copy = " + Gac2.ToString() + "\n");
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Copy failed : " + Gac1.ToString() + e);
            return false;
        }

        return true;

    }
    // ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a
    // permission with the specified state from the XML encoding.
    private bool ToFromXmlDemo()
    {
        GacIdentityPermission Gac1 = new GacIdentityPermission();
        GacIdentityPermission Gac2 = new GacIdentityPermission();
        Console.WriteLine("**************************************************************************");
        try
        {
            Gac2 = new GacIdentityPermission(PermissionState.None);
            Gac2.FromXml(Gac1.ToXml());
            bool result = Gac2.Equals(Gac1);
            if (Gac2.IsSubsetOf(Gac1) && Gac1.IsSubsetOf(Gac2))
            {
                Console.WriteLine("Result of ToFromXml = " + Gac2.ToString());
            }
            else
            {
                Console.WriteLine(Gac2.ToString());
                Console.WriteLine(Gac1.ToString());
                return false;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("ToFromXml failed. " + e);
            return false;
        }

        return true;

    }
    // Invoke all demos.
    public bool RunDemo()
    {

        bool returnCode =true;
        bool tempReturnCode;
        // Call the IsSubsetOf demo.
        if (tempReturnCode = IsSubsetOfDemo())Console.Out.WriteLine("IsSubsetOf demo completed successfully.");
        else Console.Out.WriteLine("Subset demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call the Union demo.
        if (tempReturnCode = UnionDemo())Console.Out.WriteLine("Union demo completed successfully.");
        else Console.Out.WriteLine("Union demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call the Intersect demo.
        if (tempReturnCode = IntersectDemo())Console.Out.WriteLine("Intersect demo completed successfully.");
        else Console.Out.WriteLine("Intersect demo failed.");
        returnCode = tempReturnCode && returnCode;


        // Call the Copy demo.
        if (tempReturnCode = CopyDemo())Console.Out.WriteLine("Copy demo completed successfully.");
        else Console.Out.WriteLine("Copy demo failed.");
        returnCode = tempReturnCode && returnCode;

        // Call the ToFromXML demo.
        if (tempReturnCode = ToFromXmlDemo())Console.Out.WriteLine("ToFromXML demo completed successfully.");
        else Console.Out.WriteLine("ToFromXml demo failed.");
        returnCode = tempReturnCode && returnCode;

        return (returnCode);

    }
    // Test harness.
    public static void Main(String[] args)
    {
        try
        {
            GacIdentityPermissionDemo testcase = new GacIdentityPermissionDemo();
            bool returnCode = testcase.RunDemo();
            if (returnCode)
            {
                Console.Out.WriteLine("The GacIdentityPermission demo completed successfully.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 100;
            }
            else
            {
                Console.Out.WriteLine("The GacIdentityPermission demo failed.");
                Console.Out.WriteLine("Press the Enter key to exit.");
                string consoleInput = Console.ReadLine();
                System.Environment.ExitCode = 101;
            }
        }
        catch (Exception e)
        {
            Console.Out.WriteLine("The GacIdentityPermission demo failed.");
            Console.WriteLine(e.ToString());
            Console.Out.WriteLine("Press the Enter key to exit.");
            string consoleInput = Console.ReadLine();
            System.Environment.ExitCode = 101;
        }
    }
}




import System.* ;
import System.Security.* ;
import System.Security.Permissions.*;

public class GacIdentityPermissionDemo
{
    // IsSubsetOf determines whether the current permission is a subset 
    // of the specified permission.
    private boolean IsSubsetOfDemo()
    {
        try {
            GacIdentityPermission gac1 = new GacIdentityPermission();
            GacIdentityPermission gac2 = new GacIdentityPermission(
                PermissionState.None);
            if (gac1.Equals(gac2)) {
                Console.WriteLine("GacIdentityPermission() " 
                    + "equals GacIdentityPermission(PermissionState.None).");
            }

            if (gac1.IsSubsetOf(gac2)) {
                Console.WriteLine((gac1 + " is a subset of " + gac2));
            }
            else {
                Console.WriteLine((gac1 + " is not a subset of " + gac2));
            }
        }
        catch (System.Exception e) {
            Console.WriteLine(("An exception was thrown : " + e));
            return false;
        }
        return true;
    } //IsSubsetOfDemo

    // Union creates a new permission that is the union of the current 
    // permission and the specified permission.
    private boolean UnionDemo()
    {
        GacIdentityPermission gac1 = new GacIdentityPermission(
            PermissionState.None);

        GacIdentityPermission gac2 = new GacIdentityPermission();

        try {
            GacIdentityPermission p3 = 
                ((GacIdentityPermission)(gac1.Union(gac2)));
            if (p3 != null) {
                Console.WriteLine("The union of two GacIdentityPermissions " 
                    + "was successful.");
            }
            else {
                Console.WriteLine("The union of two " 
                    + "GacIdentityPermissions failed.");
                return false;
            }
        }
        catch (System.Exception e) {
            Console.WriteLine(("An exception was thrown : " + e));
            return false;
        }
        return true;
    } //UnionDemo

    // Intersect creates and returns a new permission that is the 
    // intersection of the current permission and the specified permission.
    private boolean IntersectDemo()
    {
        GacIdentityPermission gac1 = new GacIdentityPermission();
        GacIdentityPermission gac2 = new GacIdentityPermission();

        try {
            GacIdentityPermission p3 = 
                ((GacIdentityPermission)(gac1.Intersect(gac2)));
            if (p3 != null) {
                Console.WriteLine(("The intersection of the two permissions = " 
                    + p3.ToString() + "\n"));
            }
            else {
                Console.WriteLine("The intersection of the two permissions " 
                    + "is null.\n");
            }
        }
        catch (System.Exception e) {
            Console.WriteLine(("An exception was thrown : " + e));
            return false;
        }
        return true;
    } //IntersectDemo    

    // Copy creates and returns an identical copy of the current permission.
    private boolean CopyDemo()
    {
        GacIdentityPermission gac1 = new GacIdentityPermission();
        GacIdentityPermission gac2 = new GacIdentityPermission();
        Console.WriteLine("****************************************" 
            + "**********************************");
        
        try {
            gac2 = ((GacIdentityPermission)(gac1.Copy()));
            if (gac2 != null) {
                Console.WriteLine(("Result of copy = " 
                    + gac2.ToString() + "\n"));
            }
        }
        catch (System.Exception e) {
            Console.WriteLine(("Copy failed : " + gac1.ToString() + e));
            return false;
        }
        return true;
    } //CopyDemo

    // ToXml creates an XML encoding of the permission and its current state; 
    // FromXml reconstructs a permission with the specified state from the 
    // XML encoding.
    private boolean ToFromXmlDemo()
    {
        GacIdentityPermission gac1 = new GacIdentityPermission();
        GacIdentityPermission gac2 = new GacIdentityPermission();
        Console.WriteLine("***************************************" 
            + "***********************************");
        
        try {
            gac2 = new GacIdentityPermission(PermissionState.None);
            gac2.FromXml(gac1.ToXml());

            boolean result = gac2.Equals(gac1);

            if (gac2.IsSubsetOf(gac1) && gac1.IsSubsetOf(gac2)) {
                Console.WriteLine(("Result of ToFromXml = " + gac2.ToString()));
            }
            else {
                Console.WriteLine(gac2.ToString());
                Console.WriteLine(gac1.ToString());
                return false;
            }
        }
        catch (System.Exception e) {
            Console.WriteLine(("ToFromXml failed. " + e));
            return false;
        }
        return true;
    } //ToFromXmlDemo    

    // Invoke all demos.
    public boolean RunDemo()
    {
        boolean returnCode = true;
        boolean tempReturnCode;

        // Call the IsSubsetOf demo.
        if (tempReturnCode = IsSubsetOfDemo()) {
            Console.get_Out().WriteLine("IsSubsetOf demo " 
                + "completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Subset demo failed.");
        }
        returnCode = tempReturnCode && returnCode;

        // Call the Union demo.
        if (tempReturnCode = UnionDemo()) {
            Console.get_Out().WriteLine("Union demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Union demo failed.");
        }
        returnCode = tempReturnCode && returnCode;

        // Call the Intersect demo.
        if (tempReturnCode = IntersectDemo()) {
            Console.get_Out().WriteLine("Intersect demo " 
                + "completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Intersect demo failed.");
        }
        returnCode = tempReturnCode && returnCode;

        // Call the Copy demo.
        if (tempReturnCode = CopyDemo()) {
            Console.get_Out().WriteLine("Copy demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Copy demo failed.");
        }
        returnCode = tempReturnCode && returnCode;

        // Call the ToFromXML demo.
        if (tempReturnCode = ToFromXmlDemo()) {
            Console.get_Out().WriteLine("ToFromXML demo " 
                + "completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("ToFromXml demo failed.");
        }
        returnCode = tempReturnCode && returnCode;
        return returnCode;
    } //RunDemo    

    // Test harness.
    public static void main(String[] args)
    {
        try {
            GacIdentityPermissionDemo testcase = 
                new GacIdentityPermissionDemo();
            boolean returnCode = testcase.RunDemo();
            if (returnCode) {
                Console.get_Out().WriteLine("The GacIdentityPermission demo " 
                    + "completed successfully.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                System.Environment.set_ExitCode(100);
            }
            else {
                Console.get_Out().WriteLine("The GacIdentityPermission " 
                    + "demo failed.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                System.Environment.set_ExitCode(101);
            }
        }
        catch (System.Exception e) {
            Console.get_Out().WriteLine("The GacIdentityPermission " 
                + "demo failed.");
            Console.WriteLine(e.ToString());
            Console.get_Out().WriteLine("Press the Enter key to exit.");
            String consoleInput = Console.ReadLine();
            System.Environment.set_ExitCode(101);
        }
    } //main
} //GacIdentityPermissionDemo

System.Object
   System.Security.CodeAccessPermission
    System.Security.Permissions.GacIdentityPermission
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 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.