共用方式為


CodeAccessPermission.IsSubsetOf(IPermission) 方法

定義

當由衍生類別實作時,決定目前使用權限是否為指定使用權限的子集。

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

參數

target
IPermission

要測試子集關聯性的權限。 這個權限必須與目前權限屬於相同的類型。

傳回

如果目前權限是指定權限的子集,則為 true,否則為 false

實作

例外狀況

target 參數不是 null,而且與目前權限屬於不同的類型。

範例

下列程式代碼範例顯示 方法的 IsSubsetOf 覆寫。 此程式代碼範例是針對 類別提供的較大範例的 CodeAccessPermission 一部分。

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

備註

如果目前許可權指定一組完全由指定許可權所包含的作業,則目前的許可權是指定許可權的子集。 例如,代表存取權的許可權 C:\example.txt 是代表 C:\存取權的許可權子集。 如果這個方法傳 true回 ,則目前的許可權代表對受保護資源的存取權,而不是指定的許可權。

下列語句必須是 true 方法的所有覆寫 IsSubsetOfXYZ 代表非 Null 參考的自定義程式代碼存取權限物件、 U 代表不受限制的程式代碼存取許可權, 而 N 代表具有 PermissionStateNone空白許可權。

  • X.IsSubsetOf (X) 會傳 true回 。

  • X.IsSubsetOf (Y) 傳回與 Y 相同的值。IsSubsetOf (X) 只有在 XY 代表相同的許可權集時才為 。

  • 如果 為 X。IsSubsetOf (Y) 和 Y。IsSubsetOf (Z) 都會傳回 trueX。IsSubsetOf (Z) 會傳 true回 。

  • X.IsSubsetOf (U) 會傳 true回 。

  • X.IsSubsetOf (N) 會傳 false回 。

  • N.IsSubsetOf (X) 會傳 true回 。

如果 XY 代表 Null 參考的自訂程式代碼存取權限物件, 則為 X。IsSubsetOf (Y) 會傳 true回 。 如果 Z 也是 Null,則複合集作業 X。等位 (Y) 。IsSubsetOf (Z) 也會傳回, true 因為兩個 Null 許可權的聯集是 Null 許可權。

給實施者的注意事項

您必須在衍生類別中覆寫這個方法。

適用於