CodeAccessPermission.IsSubsetOf(IPermission) Método

Definição

Quando implementado em uma classe derivada, determina se a permissão atual é um subconjunto da permissão especificada.

public:
 abstract bool IsSubsetOf(System::Security::IPermission ^ target);
public abstract bool IsSubsetOf (System.Security.IPermission target);
abstract member IsSubsetOf : System.Security.IPermission -> bool
Public MustOverride Function IsSubsetOf (target As IPermission) As Boolean

Parâmetros

target
IPermission

Uma permissão que deve ser testada quanto à relação de subconjunto. Essa permissão deve ser do mesmo tipo da permissão atual.

Retornos

Boolean

true se a permissão atual for um subconjunto da permissão especificada; caso contrário, false.

Implementações

Exceções

O parâmetro target não é null e não é do mesmo tipo que a permissão atual.

Exemplos

O exemplo de código a seguir mostra uma substituição do IsSubsetOf método. Este exemplo de código faz parte de um exemplo maior fornecido para a CodeAccessPermission classe.

public:
   virtual bool IsSubsetOf( IPermission^ target ) override
   {
#if ( debug ) 
      Console::WriteLine( "************* Entering IsSubsetOf *********************" );
#endif

      if ( target == nullptr )
      {
         Console::WriteLine( "IsSubsetOf: target == null" );
         return false;
      }

#if ( debug ) 
      Console::WriteLine( "This is = {0}", ((NameIdPermission)this).Name );
      Console::WriteLine( "Target is {0}", ((NameIdPermission)target).m_Name );
#endif

      try
      {
         NameIdPermission^ operand = dynamic_cast<NameIdPermission^>(target);
         
         // The following check for unrestricted permission is only included as an example for
         // permissions that allow the unrestricted state. It is of no value for this permission.
         if ( true == operand->m_Unrestricted )
         {
            return true;
         }
         else if ( true == this->m_Unrestricted )
         {
            return false;
         }

         if ( this->m_Name != nullptr )
         {
            if ( operand->m_Name == nullptr )
            {
               return false;
            }
            if ( this->m_Name->Equals( "" ) )
            {
               return true;
            }
         }

         if ( this->m_Name->Equals( operand->m_Name ) )
         {
            return true;
         }
         else
         {
            // Check for wild card character '*'.
            int i = operand->m_Name->LastIndexOf( "*" );

            if ( i > 0 )
            {
               String^ prefix = operand->m_Name->Substring( 0, i );
               if ( this->m_Name->StartsWith( prefix ) )
               {
                  return true;
               }
            }
         }
         return false;
      }
      catch ( InvalidCastException^ ) 
      {
         throw gcnew ArgumentException( String::Format( "Argument_WrongType", this->GetType()->FullName ) );
      }
   }
        public override bool IsSubsetOf(IPermission target)
        {
#if(debug)
            Console.WriteLine ("************* Entering IsSubsetOf *********************");
#endif
            if (target == null)
            {
                Console.WriteLine ("IsSubsetOf: target == null");
                return false;
            }
#if(debug)

            Console.WriteLine ("This is = " + (( NameIdPermission)this).Name);
            Console.WriteLine ("Target is " + (( NameIdPermission)target).m_Name);
#endif
            try
            {
                 NameIdPermission operand = ( NameIdPermission)target;

                // The following check for unrestricted permission is only included as an example for
                // permissions that allow the unrestricted state. It is of no value for this permission.
                if (true == operand.m_Unrestricted)
                {
                    return true;
                }
                else if (true == this.m_Unrestricted)
                {
                    return false;
                }

                if (this.m_Name != null)
                {
                    if (operand.m_Name == null) return false;

                    if (this.m_Name == "") return true;
                }

                if (this.m_Name.Equals (operand.m_Name))
                {
                    return true;
                }
                else
                {
                    // Check for wild card character '*'.
                    int i = operand.m_Name.LastIndexOf ("*");

                    if (i > 0)
                    {
                        string prefix = operand.m_Name.Substring (0, i);

                        if (this.m_Name.StartsWith (prefix))
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
            catch (InvalidCastException)
            {
                throw new ArgumentException (String.Format ("Argument_WrongType", this.GetType ().FullName));
            }
        }
        Public Overrides Function IsSubsetOf(ByVal target As IPermission) As Boolean

#If (Debug) Then

            Console.WriteLine("************* Entering IsSubsetOf *********************")
#End If
            If target Is Nothing Then
                Console.WriteLine("IsSubsetOf: target == null")
                Return False
            End If
#If (Debug) Then
            Console.WriteLine(("This is = " + CType(Me, NameIdPermission).Name))
            Console.WriteLine(("Target is " + CType(target, NameIdPermission).m_name))
#End If
            Try
                Dim operand As NameIdPermission = CType(target, NameIdPermission)

                ' The following check for unrestricted permission is only included as an example for
                ' permissions that allow the unrestricted state. It is of no value for this permission.
                If True = operand.m_Unrestricted Then
                    Return True
                ElseIf True = Me.m_Unrestricted Then
                    Return False
                End If

                If Not (Me.m_name Is Nothing) Then
                    If operand.m_name Is Nothing Then
                        Return False
                    End If
                    If Me.m_name = "" Then
                        Return True
                    End If
                End If
                If Me.m_name.Equals(operand.m_name) Then
                    Return True
                Else
                    ' Check for wild card character '*'.
                    Dim i As Integer = operand.m_name.LastIndexOf("*")

                    If i > 0 Then
                        Dim prefix As String = operand.m_name.Substring(0, i)

                        If Me.m_name.StartsWith(prefix) Then
                            Return True
                        End If
                    End If
                End If

                Return False
            Catch
                Throw New ArgumentException(String.Format("Argument_WrongType", Me.GetType().FullName))
            End Try
        End Function

Comentários

A permissão atual é um subconjunto da permissão especificada se a permissão atual especificar um conjunto de operações totalmente contido pela permissão especificada. Por exemplo, uma permissão que representa o acesso ao C:\example.txt é um subconjunto de uma permissão que representa o acesso ao C:\. Se esse método retornar true, a permissão atual não representará mais acesso ao recurso protegido do que a permissão especificada.

As instruções a seguir são necessárias para todas true as substituições do IsSubsetOf método. X, Y e Z representam objetos de permissão de acesso ao código personalizado que não são referências nulas, U representa uma permissão de acesso de código irrestrito e N representa uma permissão vazia com um PermissionState de None.

  • X. IsSubsetOf(X) retorna true.

  • X. IsSubsetOf(Y) retorna o mesmo valor que Y. IsSubsetOf(X) se e somente se X e Y representarem o mesmo conjunto de permissões.

  • Se X. IsSubsetOf(Y) e Y. IsSubsetOf(Z) ambos retornam true, X. IsSubsetOf(Z) retorna true.

  • X. IsSubsetOf(U) retorna true.

  • X. IsSubsetOf(N) retorna false.

  • N. IsSubsetOf(X) retorna true.

Se X e Y representarem objetos de permissão de acesso ao código personalizado que são referências nulas, X. IsSubsetOf(Y) retorna true. Se Z também for nulo, a operação X do conjunto composto. União(Y). IsSubsetOf(Z) também retorna true porque a união de duas permissões nulas é uma permissão nula.

Notas aos Implementadores

Você deve substituir esse método em uma classe derivada.

Aplica-se a