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

GacMembershipCondition Class

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

Determines whether an assembly belongs to a code group by testing its global assembly cache membership. This class cannot be inherited.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class GacMembershipCondition : IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class GacMembershipCondition implements IMembershipCondition, ISecurityEncodable, 
	ISecurityPolicyEncodable
SerializableAttribute 
ComVisibleAttribute(true) 
public final class GacMembershipCondition implements IMembershipCondition, ISecurityEncodable, 
	ISecurityPolicyEncodable

All assemblies installed in the global assembly cache are granted the FullTrust permission set.

The following example demonstrates the use of the GacMembershipCondition class.

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

public class GacMembershipConditionDemo
{
    // Demonstrate the Copy method, which creates an identical
    // copy of the current permission.
    private bool CopyDemo()
    {
        Console.WriteLine(
            "************************************************************");
        GacMembershipCondition Gac1 = new GacMembershipCondition();
        Console.WriteLine("Original membership condition = ");
        Console.WriteLine(Gac1.ToXml().ToString());
        try
        {
            IMembershipCondition membershipCondition = Gac1.Copy();
            Console.WriteLine("Result of Copy = ");
            Console.WriteLine(
                ((GacMembershipCondition)membershipCondition).ToXml().ToString()
                );
        }
        catch (Exception e)
        {
            Console.WriteLine("Copy failed : " + Gac1.ToString() + e);
            return false;
        }

        return true;

    }
    // Demonstrate the Check method, which determines whether the specified 
    // evidence satisfies the membership condition.
    private bool CheckDemo()
    {
        Console.WriteLine(
            "************************************************************");
        GacMembershipCondition Gac1 = new GacMembershipCondition();
        GacInstalled myGac = new GacInstalled();
        try
        {
            Object [] hostEvidence = {myGac};
            Object [] assemblyEvidence = {};

            Evidence myEvidence = new Evidence(hostEvidence,assemblyEvidence);
            bool retCode = Gac1.Check(myEvidence);
            Console.WriteLine("Result of Check = " + retCode.ToString() + "\n");
        }
        catch (Exception e)
        {
            Console.WriteLine("Check failed : " + Gac1.ToString() + e);
            return false;
        }

        return true;
    }

    // Demonstrate the GetHashCode method, which returns a hash code
    // for the specified membership condition.
    private bool GetHashCodeDemo()
    {
        Console.WriteLine(
            "************************************************************");
        GacMembershipCondition Gac1 = new GacMembershipCondition();
        try
        {
            Console.WriteLine(
                "Result of GetHashCode for a GacMembershipCondition = " + 
                Gac1.GetHashCode().ToString() + "\n");
        }
        catch (Exception e)
        {
            Console.WriteLine("GetHashCode failed : " + Gac1.ToString() + e);
            return false;
        }

        return true;
    }

    // Demonstrate the ToXml and FromXml methods, including the overloads.
    // ToXml creates an XML encoding of the membership condition and its 
    // current state; FromXml reconstructs a membership condition with the 
    // specified state from the XML encoding.
    private bool ToFromXmlDemo()
    {
        Console.WriteLine(
            "************************************************************");
        try
        {
            GacMembershipCondition Gac1 = new GacMembershipCondition();
            GacMembershipCondition Gac2 = new GacMembershipCondition();

            // Roundtrip a GacMembershipCondition to and from an XML encoding.
            Gac2.FromXml(Gac1.ToXml());
            bool result = Gac2.Equals(Gac1);
            if (result)
            {
                Console.WriteLine(
                    "Result of ToXml() = " + Gac2.ToXml().ToString());
                Console.WriteLine(
                    "Result of ToFromXml roundtrip = " + Gac2.ToString());
            }
            else
            {
                Console.WriteLine(Gac2.ToString());
                Console.WriteLine(Gac1.ToString());
                return false;
            }

            GacMembershipCondition Gac3 = new GacMembershipCondition();
            GacMembershipCondition Gac4 = new GacMembershipCondition();
            IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();
            while (policyEnumerator.MoveNext())
            {
                PolicyLevel currentLevel = 
                    (PolicyLevel)policyEnumerator.Current;
                if (currentLevel.Label == "Machine")
                {
                    Console.WriteLine("Result of ToXml(level) = " + 
                        Gac3.ToXml(currentLevel));
                    Gac4.FromXml(Gac3.ToXml(), currentLevel);
                    Console.WriteLine("Result of FromXml(element, level) = " + 
                        Gac4.ToString());
                }
            }
        }
        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 Copy demo.
        if (tempReturnCode = CopyDemo())
            Console.Out.WriteLine("Copy demo completed successfully.");
        else 
            Console.Out.WriteLine("Copy demo failed.");

        returnCode = tempReturnCode && returnCode;

        // Call the Check demo.
        if (tempReturnCode = CheckDemo())
            Console.Out.WriteLine("Check demo completed successfully.");
        else 
            Console.Out.WriteLine("Check demo failed.");

        returnCode = tempReturnCode && returnCode;

        // Call the GetHashCode demo.
        if (tempReturnCode = GetHashCodeDemo())
            Console.Out.WriteLine("GetHashCode demo completed successfully.");
        else 
            Console.Out.WriteLine("GetHashCode 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
        {
            GacMembershipConditionDemo testcase = 
                new GacMembershipConditionDemo();
            bool returnCode = testcase.RunDemo();
            if (returnCode)
            {
                Console.Out.WriteLine(
                    "The GacMembershipCondition 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 GacMembershipCondition 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.Policy.*;
import System.Collections.*;
import System.Security.SecurityManager;

public class GacMembershipConditionDemo
{
    // Demonstrate the Copy method, which creates an identical copy of the
    // current permission.
    private boolean CopyDemo()
    {
        Console.WriteLine("********************************************"
            + "*******");

        GacMembershipCondition gac1 = new GacMembershipCondition();
        Console.WriteLine("Original membership condition = ");
        Console.WriteLine(gac1.ToXml().ToString());
        try {
            IMembershipCondition membershipCondition = gac1.Copy();
            Console.WriteLine("Result of Copy = ");
            Console.WriteLine(((GacMembershipCondition)(membershipCondition)).
                ToXml().ToString());
        }
        catch (System.Exception e) {
            Console.WriteLine(("Copy failed : " + gac1.ToString() + e));
            return false;
        }
        return true;
    } //CopyDemo

    // Demonstrate the Check method, which determines whether the specified 
    // evidence satisfies the membership condition.
    private boolean CheckDemo()
    {
        Console.WriteLine("*******************************************"
            + "********");

        GacMembershipCondition gac1 = new GacMembershipCondition();
        GacInstalled myGac = new GacInstalled();

        try {
            Object hostEvidence[] = new Object[] { myGac };
            Object assemblyEvidence[] = null;
            Evidence myEvidence = new Evidence(hostEvidence, assemblyEvidence);
            boolean retCode = gac1.Check(myEvidence);
            Console.WriteLine(("Result of Check = " + 
                System.Convert.ToString(retCode) 
                + "\n"));
        }
        catch (System.Exception e) {
            Console.WriteLine(("Check failed : " + gac1.ToString() + e));
            return false;
        }
        return true;
    } //CheckDemo

    // Demonstrate the GetHashCode method which returns a hash code for 
    // the specified membership condition.
    private boolean GetHashCodeDemo()
    {
        Console.WriteLine("*********************************************"
            + "******");

        GacMembershipCondition gac1 = new GacMembershipCondition();

        try {
            Console.WriteLine(
                ("Result of GetHashCode for a GacMembershipCondition = " 
                + gac1.GetHashCode() + "\n"));
        }
        catch (System.Exception e) {
            Console.WriteLine(("GetHashCode failed : " + gac1.ToString() + e));
            return false;
        }
        return true;
    } //GetHashCodeDemo

    // Demonstrate the ToXml and FromXml methods, including the overloads. 
    // ToXml creates an XML encoding of  the membership condition and its 
    // current state; FromXml reconstructs a membership condition with the 
    // specified state from the XML encoding.
    private boolean ToFromXmlDemo()
    {
        Console.WriteLine("********************************************"
            + "*******");
        
        try {
            GacMembershipCondition gac1 = new GacMembershipCondition();
            GacMembershipCondition gac2 = new GacMembershipCondition();

            // Roundtrip a GacMembershipCondition to and from an XML encoding.
            gac2.FromXml(gac1.ToXml());
            boolean result = gac2.Equals(gac1);
            if (result) {
                Console.WriteLine(("Result of ToXml() = " 
                    + gac2.ToXml().ToString()));
                Console.WriteLine(("Result of ToFromXml roundtrip = " 
                    + gac2.ToString()));
            }
            else {
                Console.WriteLine(gac2.ToString());
                Console.WriteLine(gac1.ToString());
                return false;
            }

            GacMembershipCondition gac3 = new GacMembershipCondition();
            GacMembershipCondition gac4 = new GacMembershipCondition();
            IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();

            while (policyEnumerator.MoveNext()) {
                PolicyLevel currentLevel = ((PolicyLevel)(policyEnumerator.
                    get_Current()));
                if (currentLevel.get_Label().Equals("Machine")) {
                    Console.WriteLine(("Result of ToXml(level) = " 
                        + gac3.ToXml(currentLevel)));
                    gac4.FromXml(gac3.ToXml(), currentLevel);
                    Console.WriteLine(("Result of FromXml(element, level) = " 
                        + gac4.ToString()));
                }
            }
        }
        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 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 Check demo.
        if (tempReturnCode = CheckDemo()) {
            Console.get_Out().WriteLine("Check demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("Check demo failed.");
        }
        returnCode = tempReturnCode && returnCode;

        // Call the GetHashCode demo.
        if (tempReturnCode = GetHashCodeDemo()) {
            Console.get_Out().WriteLine(
                "GetHashCode demo completed successfully.");
        }
        else {
            Console.get_Out().WriteLine("GetHashCode 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 {
            GacMembershipConditionDemo testcase = 
                new GacMembershipConditionDemo();
            boolean returnCode = testcase.RunDemo();

            if (returnCode) {
                Console.get_Out().WriteLine(
                    "The GacMembershipCondition demo" 
                    + " completed successfully.");
                Console.get_Out().WriteLine("Press the Enter key to exit.");
                String consoleInput = Console.ReadLine();
                Environment.set_ExitCode(100);
            }
            else {
                Console.get_Out().WriteLine("The GacMembershipCondition " 
                    + " demo failed.");
                Console.get_Out().WriteLine("Press the ENTER key to exit.");
                String consoleInput = Console.ReadLine();
                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();
            Environment.set_ExitCode(101);
        }
    } //main
} //GacMembershipConditionDemo

System.Object
  System.Security.Policy.GacMembershipCondition
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.