Esta página foi útil?
Seus comentários sobre este conteúdo são importantes. Queremos saber sua opinião.
Comentários adicionais?
1500 caracteres restantes
Interface IUnrestrictedPermission
Collapse the table of content
Expand the table of content
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original

Interface IUnrestrictedPermission

Concede uma permissão para expor um estado irrestrito.

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

[ComVisibleAttribute(true)]
public interface IUnrestrictedPermission

Todas as permissões de acesso ao código devem implementar IUnrestrictedPermission.

Este exemplo mostra como definir uma classe de permissão para ser usado com segurança de acesso do código.Todas as interfaces necessárias permissão são implementadas.

O exemplo de código a seguir demonstra o comportamento do IUnrestrictedPermission métodos.

O exemplo destina-se a mostrar como executam os métodos que se você executar os métodos do seu código.Em geral, os métodos das classes de permissão são usados pela infra-estrutura de segurança; eles não são normalmente usados em aplicativos.

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

// Enumerated type for permission states.
[Serializable]
publicenum SoundPermissionState 
{
    NoSound = 0,
    PlaySystemSounds = 1,
    PlayAnySound = 2
}

// Derive from CodeAccessPermission to gain implementations of the following// sealed IStackWalk methods: Assert, Demand, Deny, and PermitOnly.// Implement the following abstract IPermission methods: Copy, Intersect, and IsSubSetOf.// Implementing the Union method of the IPermission class is optional.// Implement the following abstract ISecurityEncodable methods: FromXml and ToXml.// Making the class 'sealed' is optional.publicsealedclass SoundPermission : CodeAccessPermission, IPermission, 
    IUnrestrictedPermission, ISecurityEncodable, ICloneable 
{
    private Boolean m_specifiedAsUnrestricted = false;
    private SoundPermissionState m_flags = SoundPermissionState.NoSound;

    // This constructor creates and initializes a permission with generic access.public SoundPermission(PermissionState state)
    {
        m_specifiedAsUnrestricted = (state == PermissionState.Unrestricted);
    }

    // This constructor creates and initializes a permission with specific access.        public SoundPermission(SoundPermissionState flags) 
    {
        if (!Enum.IsDefined(typeof(SoundPermissionState), flags))
            thrownew ArgumentException
                ("flags value is not valid for the SoundPermissionState enuemrated type");
        m_specifiedAsUnrestricted = false;
        m_flags = flags;
    }

    // For debugging, return the state of this object as XML.publicoverride String ToString() { return ToXml().ToString(); }

    // Private method to cast (if possible) an IPermission to the type.private SoundPermission VerifyTypeMatch(IPermission target) 
    {
        if (GetType() != target.GetType())
            thrownew ArgumentException(String.Format("target must be of the {0} type",
                GetType().FullName));
        return (SoundPermission) target;
    }

    // This is the Private Clone helper method. private SoundPermission Clone(Boolean specifiedAsUnrestricted, SoundPermissionState flags) 
    {
        SoundPermission soundPerm = (SoundPermission) Clone();
        soundPerm.m_specifiedAsUnrestricted = specifiedAsUnrestricted;
        soundPerm.m_flags = specifiedAsUnrestricted ? SoundPermissionState.PlayAnySound : m_flags;
        return soundPerm;
    }

    #region IPermission Members
    // Return a new object that contains the intersection of 'this' and 'target'.publicoverride IPermission Intersect(IPermission target) 
    {
        // If 'target' is null, return null.if (target == null) returnnull;

        // Both objects must be the same type.
        SoundPermission soundPerm = VerifyTypeMatch(target);

        // If 'this' and 'target' are unrestricted, return a new unrestricted permission.if (m_specifiedAsUnrestricted && soundPerm.m_specifiedAsUnrestricted)
            return Clone(true, SoundPermissionState.PlayAnySound);

        // Calculate the intersected permissions. If there are none, return null.
        SoundPermissionState val = (SoundPermissionState)
            Math.Min((Int32) m_flags, (Int32) soundPerm.m_flags);
        if (val == 0) returnnull;

        // Return a new object with the intersected permission value.return Clone(false, val);
    }

    // Called by the Demand method: returns true if 'this' is a subset of 'target'.publicoverride Boolean IsSubsetOf(IPermission target) 
    {
        // If 'target' is null and this permission allows nothing, return true.if (target == null) return m_flags == 0;

        // Both objects must be the same type.
        SoundPermission soundPerm = VerifyTypeMatch(target);

        // Return true if the permissions of 'this' is a subset of 'target'.return m_flags <= soundPerm.m_flags;
    }

    // Return a new object that matches 'this' object's permissions.publicsealedoverride IPermission Copy() 
    {
        return (IPermission) Clone();
    }

    // Return a new object that contains the union of 'this' and 'target'.// Note: You do not have to implement this method. If you do not, the version// in CodeAccessPermission does this://    1. If target is not null, a NotSupportedException is thrown.//    2. If target is null, then Copy is called and the new object is returned.publicoverride IPermission Union(IPermission target) 
    {
        // If 'target' is null, then return a copy of 'this'.if (target == null) return Copy();

        // Both objects must be the same type.
        SoundPermission soundPerm = VerifyTypeMatch(target);

        // If 'this' or 'target' are unrestricted, return a new unrestricted permission.if (m_specifiedAsUnrestricted || soundPerm.m_specifiedAsUnrestricted)
            return Clone(true, SoundPermissionState.PlayAnySound);

        // Return a new object with the calculated, unioned permission value.return Clone(false, (SoundPermissionState)
            Math.Max((Int32) m_flags, (Int32) soundPerm.m_flags));
    }
    #endregion

    #region ISecurityEncodable Members
    // Populate the permission's fields from XML.publicoverridevoid FromXml(SecurityElement e) 
    {
        m_specifiedAsUnrestricted = false;
        m_flags = 0;

        // If XML indicates an unrestricted permission, make this permission unrestricted.
        String s = (String) e.Attributes["Unrestricted"];
        if (s != null) 
        {
            m_specifiedAsUnrestricted = Convert.ToBoolean(s);
            if (m_specifiedAsUnrestricted)
                m_flags = SoundPermissionState.PlayAnySound;
        }

        // If XML indicates a restricted permission, parse the flags.if (!m_specifiedAsUnrestricted) 
        {
            s = (String) e.Attributes["Flags"];
            if (s != null) 
            {
                m_flags = (SoundPermissionState)
                Convert.ToInt32(Enum.Parse(typeof(SoundPermission), s, true));
            }
        }
    }

    // Produce XML from the permission's fields.publicoverride SecurityElement ToXml() {
        // These first three lines create an element with the required format.
        SecurityElement e = new SecurityElement("IPermission");
        // Replace the double quotation marks () with single quotation marks ()// to remain XML compliant when the culture is not neutral.
        e.AddAttribute("class", GetType().AssemblyQualifiedName.Replace('\"', '\''));
        e.AddAttribute("version", "1");

        if (!m_specifiedAsUnrestricted)
            e.AddAttribute("Flags", Enum.Format(typeof(SoundPermission), m_flags, "G"));
        else
            e.AddAttribute("Unrestricted", "true");
        return e;
    }
    #endregion

    #region IUnrestrictedPermission Members
    // Returns true if permission is effectively unrestricted.public Boolean IsUnrestricted() 
    {
        // This means that the object is unrestricted at runtime.return m_flags == SoundPermissionState.PlayAnySound;
    }
    #endregion

    #region ICloneable Members

    // Return a copy of the permission.public Object Clone() { return MemberwiseClone(); }

    #endregion
}


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.

.NET Framework

Compatível com: 3.5, 3.0, 2.0, 1.1, 1.0

Contribuições da comunidade

ADICIONAR
Mostrar:
© 2015 Microsoft