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

将要测试子集关系的权限。 此权限必须与当前权限属于同一类型。

返回

Boolean

如果当前权限是指定权限的子集,则为 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,则当前权限表示对受保护资源的访问权限不超过指定的权限。

对于该方法的所有重写IsSubsetOf,需要true以下语句。 XYZ 表示非空引用的自定义代码访问权限对象,U 表示不受限制的代码访问权限,N 表示具有以下NonePermissionState的空权限。

  • 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 表示空引用的自定义代码访问权限对象, 则为 X。IsSubsetOf (Y) 返回 true。 如果 Z 也为 null,则复合集运算 X。联合 (Y) 。IsSubsetOf (Z) 也返回 true ,因为两个 null 权限的联合是 null 权限。

实施者说明

必须在派生类中重写此方法。

适用于